My computer motherboard seems to be in trouble, often with computer time disorder, resulting in web pages can not be opened (actually related to certificates). Replacement of new batteries is ineffective, and do not want to pay for the motherboard. Think about it, it is very troublesome to set the time each time. So I wrote a networking time setting tool. As long as I set it to start automatically, I can automatically synchronize the time after starting and connecting to the network (no networking will wait for networking). With source code and compiled tools, the computer needs. net framework 4.0 support!
Share ideas about the program
1. Getting Time from the Internet
Preliminary assumption is that there should be an interface to provide time on the internet, so search on the internet. Really found one, Suning's time interface.
http://quan.suning.com/getSysTime.do
It's easy to get the time. Now we need to check the status of the network. Only when the computer has been connected to the Internet can we go to the interface to get the data.
2. Obtaining the State of Computer Networking
windows has the corresponding api directly on the code
[DllImport("wininet")]
//The method of judging the network condition is that the return value true is connected and false is unconnected.
public extern static bool InternetGetConnectedState(out int conState, int reder);public static bool IsNeting
{
get
{
int i = 0;
return InternetGetConnectedState(out i, 0);
}
}//Need to use System. Runtime. InteropServices;
3. Setting Time
C# sets the time by using the API provided by WINDOWS
//Call Kernel32.DLL
[DllImport("Kernel32.dll")]
public static extern void SetLocalTime(SystemTime st);
[StructLayout(LayoutKind.Sequential)]
public class 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;
}SystemTime st = new SystemTime();
st.wYear = (ushort)dt.Year;
st.wMonth = (ushort)dt.Month;
st.wDay = (ushort)dt.Day;
st.Whour = (ushort)dt.Hour;
st.wMinute = (ushort)dt.Minute;
st.wSecond = (ushort)dt.Second;
SetLocalTime(st);//Need to use System. Runtime. InteropServices;
This gadget uses a few relatively simple techniques, others can see for themselves.
Tools and source code download:
It has been uploaded to CSDN but has not passed the audit. I will update it after the audit.