C # From the network acquisition time update this machine time

xiaoxiao2021-03-06  72

Due to the problem of the main board, it is lost time, so I think a method is to get the latest time from the Internet, so I don't need to go to the time, the network is all, the time can automatically correct.

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; using System.Text; using System.IO; using System. Runtime.InteropServices;

Namespace AutoExitWindows {///

/// ResetLocalTimeFromNetwork's summary description. /// public class ReSetLocalTimeFromNetwork {[DllImport ( "kernel32", SetLastError = true)] static extern int SetSystemTime (ref SYSTEMTIME lpSystemTime); [DllImport ( "kernel32", SetLastError = true)] static extern int GetLastError ( ); // public Declare Function GetLastError Lib "kernel32" Alias ​​"GetLastError" () As Long // public Declare Function SetSystemTime Lib "kernel32" Alias ​​"SetSystemTime" (lpSystemTime As SYSTEMTIME) As Long public struct SYSTEMTIME {short wYear; short wMonth ; short wDayOfWeek; short wDay; short wHour; short wMinute; short wSecond; short wMilliseconds; public SYSTEMTIME (short year, short month, short dayofweek, short day, short hour, short minute, short second, short milliseconds) {wYear = year WMONTH = Month; wdayofweek = dayofweek; wday = day; whour = hour; wminute = minute; wsecond = second; wmilliseconds = Milliseconds;}}

public ReSetLocalTimeFromNetwork (string Server, string Port, string TimeZone) {try {// get the Network Time SNTPTimeClient client = new SNTPTimeClient (Server, Port); client.Connect (); string strTest = client.ToString (); Console.Write ( strTest);

// Remove the year, month, day, hour, minute, second, microsecond, int ModMonth, INTDAYOFWEEK, INTDAY, INTHOUR, INTMINUTE, INTSECOND, INTMILLISECONDS; Char [] split = new char [1]; split [0] = Convert.TOCHAR ("/ n"); string [] temp = strTest.split (split, 100); string strdate = ""; int idos = -1; for (int i = 0; i

// Setting up local time // When setting, it should be Greenwich Time DateTime DTMDATE = Convert.TodateTime (strdate); DTMDATE = DTMDATE.ADDHOURS ((- 1) * Convert.Toint16 (Timezone)) ; intYear = Convert.ToInt16 (dtmDate.Year); intMonth = Convert.ToInt16 (dtmDate.Month); intDayofWeek = Convert.ToInt16 (dtmDate.DayOfWeek); intDay = Convert.ToInt16 (dtmDate.Day); intHour = Convert.ToInt16 (DTMDATE. Hour); INTMINUTE = Convert.Toint16 (dtmdate.minute); intSecond = Convert.Toint16 (dtmdate.second); INTMILLISECONDS = Convert.Toint16 (DTMDate.MilliseCond);

SYSTEMTIME systime = new SYSTEMTIME (intYear, intMonth, intDayofWeek, intDay, intHour, intMinute, intSecond, intMilliseconds); int intValue = SetSystemTime (ref systime); if (intValue == 0) {intValue = GetLastError (); throw new Exception ( INTVALUE.TOSTRING () "Error, can't set local time.");} //messagebox.show (intValue.tostring () "/ n" DateTime.now.toString () "/ n" STRDATE);} catch (exception E1) {throw E1;}}}}

When calling: resetLocalTimeFromNetwork Objresettime = New ResetLocalTimeFromNetWork (Strremoteip, StrremotEport, Strtimezone);

转载请注明原文地址:https://www.9cbs.com/read-97841.html

New Post(0)