基本信息
源码名称:源码:获取本地网络信息(ip/mac/计算机名/dns等)以及网络对时(校对时间)
源码大小:2.54M
文件格式:.rar
开发语言:C#
更新时间:2019-12-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


主要功能如下:

1)本地网络信息的获取

计算机名称、IP地址、网卡地址、DNS设置、安装的协议、提供的服务、TCP/IP运行信息。

2)修改本地计算机的网络设置

3)采用ntp协议从internet上某个时间服务器,获取时间信息,准确地更新本地机时钟,支持手动、定时自动两种方式

可使用VS 2012运行此程序。







class TimeUtil
    {
        // 获取本地时间
        public static DateTime getLocalTime()
        {
            SystemTime sysTime = new SystemTime();
            TimeAPI.GetLocalTime(ref sysTime);
            return SystemTime2DateTime(sysTime);
        }

        // 设置本地时间
        public static bool setLocalTime(DateTime dateTime)
        {
            if (PrivilegeUtil.grantPrivilege(PrivilegeConstants.SE_SYSTEMTIME_NAME))
            {
                SystemTime sysTime = DateTime2SystemTime(dateTime);
                bool success = TimeAPI.SetLocalTime(ref sysTime);
                if (!PrivilegeUtil.revokePrivilege(PrivilegeConstants.SE_SYSTEMTIME_NAME))
                {
                    Program.writeMsg("撤权失败: 更改系统时间");
                }
                return success;
            }
            Program.writeMsg("授权失败: 更改系统时间");
            return false;
        }

        // 获取系统时间
        public static DateTime getSystemTime()
        {
            SystemTime sysTime = new SystemTime();
            TimeAPI.GetSystemTime(ref sysTime);
            return SystemTime2DateTime(sysTime);
        }

        // 获取网络时间
        public static DateTime getWebTime()
        {
            // default ntp server
            const string ntpServer = "ntp1.aliyun.com";

            // NTP message size - 16 bytes of the digest (RFC 2030)
            byte[] ntpData = new byte[48];
            // Setting the Leap Indicator, Version Number and Mode values
            ntpData[0] = 0x1B; // LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)

            IPAddress[] addresses = Dns.GetHostEntry(ntpServer).AddressList;
            // The UDP port number assigned to NTP is 123
            IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123);

            // NTP uses UDP
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Connect(ipEndPoint);
            // Stops code hang if NTP is blocked
            socket.ReceiveTimeout = 3000;
            socket.Send(ntpData);
            socket.Receive(ntpData);
            socket.Close();

            // Offset to get to the "Transmit Timestamp" field (time at which the reply 
            // departed the server for the client, in 64-bit timestamp format."
            const byte serverReplyTime = 40;
            // Get the seconds part
            ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
            // Get the seconds fraction
            ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime 4);
            // Convert From big-endian to little-endian
            intPart = swapEndian(intPart);
            fractPart = swapEndian(fractPart);
            ulong milliseconds = (intPart * 1000) ((fractPart * 1000) / 0x100000000UL);

            // UTC time
            DateTime webTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);
            // Local time
            return webTime.ToLocalTime();
        }