C# 同步更新系统时间

前言

在定位用户问题时,发现有些电脑,会出现系统时间不是最新的问题。

可能原因:

  1. 取消了勾选服务器时间同步
  2. 当前安装的系统,是一个未知来源系统,导致系统时间更新失败

而系统时间不正确,会导致IE选项-证书,校验不通过~

更新系统时间

1. 连接时间服务器

时间服务器列表(推荐): string[] timeHosts = { "time.windows.com", "time.nist.gov" };

 1     /// <summary>
 2     /// 连接时间服务器
 3     /// </summary>
 4     /// <param name="socket">服务器接口</param>
 5     /// <param name="startTime">开始时间</param>
 6     /// <param name="errorMsg">错误信息</param>
 7     /// <returns></returns>
 8     private static bool TryConnectToTimeServer(out Socket socket, out DateTime startTime, out string errorMsg)
 9     {
10         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket   
11         socket.ReceiveTimeout = 10 * 1000;//设置超时时间   
12         errorMsg = string.Empty;
13         startTime = DateTime.Now;
14 
15         // 遍历时间服务器列表   
16         foreach (string strHost in timeHosts)
17         {
18             try
19             {
20                 // 记录开始的时间   
21                 startTime = DateTime.Now;
22 
23                 var iphostinfo = Dns.GetHostEntry(strHost);
24                 var ip = iphostinfo.AddressList[0];
25                 //建立IPAddress对象与端口,创建IPEndPoint节点:   
26                 int port = 13;
27                 var ipe = new IPEndPoint(ip, port);
28                 //连接到服务器   
29                 socket.Connect(ipe);
30                 // 如果连接到服务器就跳出  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C# 同步网络时间的代码: ``` using System; using System.Runtime.InteropServices; public static class SystemTime { [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetSystemTime(ref SYSTEMTIME st); [StructLayout(LayoutKind.Sequential)] public struct SYSTEMTIME { public ushort wYear; public ushort wMonth; public ushort wDayOfWeek; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort wMilliseconds; } public static void SyncTimeWithServer(string ntpServer) { var ntpData = new byte[48]; ntpData[0] = 0x1B; var addresses = System.Net.Dns.GetHostEntry(ntpServer).AddressList; var ipEndPoint = new System.Net.IPEndPoint(addresses[0], 123); var socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp); socket.Connect(ipEndPoint); socket.Send(ntpData); socket.Receive(ntpData); socket.Close(); var intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | ntpData[43]; var fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | ntpData[47]; var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L); var networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds); SetSystemTime(ref networkDateTime); } } ``` 使用方法如下: ``` SystemTime.SyncTimeWithServer("pool.ntp.org"); ``` 此方法会将计算机的系统时间设置为从 NTP 服务器获取的时间。需要注意的是,该方法需要在拥有管理员权限的用户账户下运行,否则会抛出权限不足的异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值