Foreword
When users locate the problem and found some computers, the problem is not the latest system time will appear.
possible reason:
- Deselected server time synchronization
- The currently installed system is a system of unknown origin, cause the system time update failed
The system time is incorrect, would cause IE options - certificate, verification is not through ~
Update the system time
1. Connect server time
Time server list (recommended): string [] timeHosts = { "time.windows.com", "time.nist.gov"};
. 1 /// <Summary> 2 /// connection time server . 3 /// </ Summary> . 4 /// <param name = "Socket"> Server Interface </ param> . 5 /// <param name = "the startTime "> start time </ param> . 6 /// <param name =" errorMsg "> error </ param> . 7 /// <Returns> </ Returns> . 8 Private static BOOL TryConnectToTimeServer ( OUT the Socket Socket, OUT the DateTime the startTime ,out string errorMsg) 9 { 10 = Socket new new the Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Create the Socket . 11 socket.ReceiveTimeout = 10 * 1000 ; // set the timeout 12 is errorMsg = String .Empty; 13 is the startTime = the DateTime.Now; 14 15 // traversal time server list 16 the foreach ( String strHost in timeHosts) . 17 { 18 is the try . 19 { 20 is //Recording start time 21 is the startTime = the DateTime.Now; 22 is 23 is var iphostinfo = Dns.GetHostEntry (strHost); 24 var IP = iphostinfo.AddressList [ 0 ]; 25 // establish IPAddress and port objects, create IPEndPoint node: 26 is int Port = 13 is ; 27 var IPE = new new the IPEndPoint (IP, Port); 28 // connect to the server 29 Socket.connect (IPE); 30 // if the connection to the server out 31 is IF (socket.Connected) BREAK ; 32 } 33 is the catch (Exception EX) 34 is { 35 errorMsg = $ " time server connection failure / r error message:! {Ex.Message} prompted " ; 36 } 37 [ } 38 is return socket.Connected; 39 }
2. Receive data from the server
. 1 /// <Summary> 2 /// receives data from the server . 3 /// </ Summary> . 4 /// <param name = "Socket"> </ param> . 5 /// <Returns> </ Returns> . 6 Private static the StringBuilder ReceiveMessageFromServer (the Socket Socket) . 7 { . 8 // SOCKET as sync data . 9 byte [] = receiveBytes new new byte [ 1024 ]; 10 int the nBytes, nTotalBytes = 0 ; . 11 the StringBuilder SB = new new the StringBuilder (); 12 System.Text.Encoding encoding = Encoding.UTF8; 13 14 while ((nBytes = socket.Receive(receiveBytes, 0, 1024, SocketFlags.None)) > 0) 15 { 16 nTotalBytes += nBytes; 17 sb.Append(encoding.GetString(receiveBytes, 0, nBytes)); 18 } 19 20 return sb; 21 }
3. Update the local time
1 /// <summary> 2 /// 更新系统时间 3 /// </summary> 4 /// <returns>更新结果</returns> 5 public static string UpdateSystemTime() 6 { 7 try 8 { 9 var connected = TryConnectToTimeServer(out Socket socket, out var startTime, out string errorMsg); 10 if (connected) 11 { 12 var receivedMsg = ReceiveMessageFromServer(socket); 13 socket.Close(); 14 //切割字符串 15 string[] receiveMsgList = receivedMsg.ToString().Split(' '); 16 if (receiveMsgList.Length >= 3) 17 { 18 var dateTimeValue = receiveMsgList[1] + " " + receiveMsgList[2]; 19 SetLocalTime(startTime, dateTimeValue); 20 } 21 } 22 else 23 is { 24 return errorMsg; 25 } 26 is } 27 the catch (Exception E) 28 { 29 return $ " function {nameof (UpdateSystemTime)} abnormality, e.Message {} " ; 30 } 31 is return " time synchronized " ; 32 } 33 is /// <Summary> 34 is /// set the system time 35 /// </ Summary> 36 /// <param name = "the startTime"> start time of the server request</ param> 37 [ /// <param name = "DateTimeValue"> server returns time </ param> 38 is Private static void SetLocalTime (the startTime the DateTime, String DateTimeValue) 39 { 40 // get the time to now consumed 41 K = the DateTime.Now the TimeSpan - the startTime; 42 is // subtracting time consuming way 43 is the DateTime = updatedUtcTime to Convert.ToDateTime (DateTimeValue) .Subtract (- K); 44 is 45 // dispose GMT +8 46 is var updatedTime = updatedUtcTime .AddHours ( . 8 ); 47 48 // convert to System.DateTime SystemTime 49 SystemTime SYSTEMTIME = new new SystemTime (); 50 systemTime.FromDateTime (updatedTime); 51 is 52 is // Win32 API calls to set the system time 53 is Win32API.SetLocalTime ( REF SYSTEMTIME); 54 is }
System Time auxiliary class & Win32API:
1 /// <summary> 2 /// 系统时间帮助类 3 /// </summary> 4 public struct SystemTime 5 { 6 public ushort wYear; 7 public ushort wMonth; 8 public ushort wDayOfWeek; 9 public ushort wDay; 10 public ushort wHour; 11 public ushort wMinute; 12 public ushort wSecond; 13 public ushort wMilliseconds; 14 15 /// <summary> 16 /// 从System.DateTime转换。 17 /// </summary> 18 /// <param name="time">System.DateTime类型的时间。</param> 19 public void FromDateTime(DateTime time) 20 { 21 wYear = (ushort)time.Year; 22 wMonth = (ushort)time.Month; 23 wDayOfWeek = (ushort)time.DayOfWeek; 24 wDay = (ushort)time.Day; 25 wHour = (ushort)time.Hour; 26 wMinute = (ushort)time.Minute; 27 wSecond = (ushort)time.Second; 28 wMilliseconds = (ushort)time.Millisecond; 29 } 30 /// <summary> 31 /// 转换为System.DateTime类型。 32 /// </summary> 33 /// <returns></returns> 34 public DateTime ToDateTime() 35 { 36 return newThe DateTime (wYear, wMonth, WDAY, wHour, wMinute, wSecond, wMilliseconds); 37 [ } 38 is /// <Summary> 39 /// static method. Convert type System.DateTime. 40 /// </ Summary> 41 is /// <param name = "Time"> the SYSTEMTIME type of time. </ param> 42 is /// <Returns> </ Returns> 43 is public static the DateTime ToDateTime (SystemTime Time) 44 is { 45 return time.ToDateTime (); 46 is } 47 } 48 49 /// <Summary> 系统更新时间DLL 51 /// </summary> 52 public class Win32API 53 { 54 [DllImport("Kernel32.dll")] 55 public static extern bool SetLocalTime(ref SystemTime Time); 56 [DllImport("Kernel32.dll")] 57 public static extern void GetLocalTime(ref SystemTime Time); 58 }
Github Address: IE repair tool environment