需要用到的名空间
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
建立一个结构
- 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 void FromDateTime(DateTime time)
- {
- wYear = (ushort)time.Year;
- wMonth = (ushort)time.Month;
- wDayOfWeek = (ushort)time.DayOfWeek;
- wDay = (ushort)time.Day;
- wHour = (ushort)time.Hour;
- wMinute = (ushort)time.Minute;
- wSecond = (ushort)time.Second;
- wMilliseconds = (ushort)time.Millisecond;
- }
-
-
-
-
- public DateTime ToDateTime()
- {
- return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
- }
-
-
-
-
-
- public static DateTime ToDateTime(SystemTime time)
- {
- return time.ToDateTime();
- }
- }
要用到Windows的API函数来设置系统时间
- public class Win32API
- {
- [DllImport("Kernel32.dll")]
- public static extern bool SetLocalTime(ref SystemTime Time);
- [DllImport("Kernel32.dll")]
- public static extern void GetLocalTime(ref SystemTime Time);
- }
用Socket获取Internet时间服务器上的时间
- public void SetInternetTime()
- {
-
- DateTime startDT = DateTime.Now;
-
-
- int port = 13;
- string[] whost = { "5time.nist.gov", "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov", "tick.mit.edu", "time.windows.com", "clock.sgi.com" };
-
- IPHostEntry iphostinfo;
- IPAddress ip;
- IPEndPoint ipe;
- Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-
- c.ReceiveTimeout = 10 * 1000;
-
- string sEX = "";
-
-
- foreach (string strHost in whost)
- {
- try
- {
- iphostinfo = Dns.GetHostEntry(strHost);
- ip = iphostinfo.AddressList[0];
- ipe = new IPEndPoint(ip, port);
-
- c.Connect(ipe);
- if (c.Connected) break;
- }
- catch (Exception ex)
- {
- sEX = ex.Message;
- }
- }
-
- if (!c.Connected)
- {
- MessageBox.Show("时间服务器连接失败!/r错误信息:" + sEX, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
-
-
- byte[] RecvBuffer = new byte[1024];
- int nBytes, nTotalBytes = 0;
- StringBuilder sb = new StringBuilder();
- System.Text.Encoding myE = Encoding.UTF8;
-
- while ((nBytes = c.Receive(RecvBuffer, 0, 1024, SocketFlags.None)) > 0)
- {
- nTotalBytes += nBytes;
- sb.Append(myE.GetString(RecvBuffer, 0, nBytes));
- }
-
-
- c.Close();
-
- string[] o = sb.ToString().Split(‘ ‘);
-
- textBox1.Text = sb.ToString();
-
- TimeSpan k = new TimeSpan();
- k = (TimeSpan)(DateTime.Now - startDT);
-
- DateTime SetDT = Convert.ToDateTime(o[1] + " " + o[2]).Subtract(-k);
-
-
- SetDT = SetDT.AddHours(8);
-
-
- SystemTime st = new SystemTime();
- st.FromDateTime(SetDT);
-
-
- Win32API.SetLocalTime(ref st);
-
- MessageBox.Show("时间已同步", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
这个东西是收集网上的一些做法再修改了一下
用vs2008+windows xp sp2测试通过
但是始终会有±1秒的误差,但大部分误差在1秒以下,尚可接受
使用的名空间包括vs自己添加的,windows Form中用到的那部分
如果换了环境,可作相应修改
转载:http://blog.csdn.net/zhengxia19/article/details/3858910