需要用到的名空间

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;/// <summary>/// 从System.DateTime转换。/// </summary>/// <param name="time">System.DateTime类型的时间。</param>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;}/// <summary>/// 转换为System.DateTime类型。/// </summary>/// <returns></returns>public DateTime ToDateTime(){return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);}/// <summary>/// 静态方法。转换为System.DateTime类型。/// </summary>/// <param name="time">SYSTEMTIME类型的时间。</param>/// <returns></returns>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;//建立IPAddress对象与端口,创建IPEndPoint节点: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);//创建Socketc.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;}//SOCKET同步接受数据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);// 减去中途消耗的时间//处置北京时间 +8时SetDT = SetDT.AddHours(8);//转换System.DateTime到SystemTimeSystemTime st = new SystemTime();st.FromDateTime(SetDT);//调用Win32 API设置系统时间Win32API.SetLocalTime(ref st);MessageBox.Show("时间已同步", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}

这个东西是收集网上的一些做法再修改了一下

用vs2008+windows xp sp2测试通过

但是始终会有±1秒的误差,但大部分误差在1秒以下,尚可接受

使用的名空间包括vs自己添加的,windows Form中用到的那部分

如果换了环境,可作相应修改

c# Internet时间服务器同步相关推荐

  1. 计算机时间与网络时间无法同步,终于解决了本地计算机时间无法与Internet时间服务器同步的难题...

    一:如何更新 本地计算机时间与Internet时间服务器同步的操作方法如下,首先在任务栏右边那里找到时间图标,直接双击,弹出如下的对话框: 如上图,切换到"Internet 时间" ...

  2. 自动与Internet时间服务器同步

    自动与Internet时间服务器同步 Windows默认的二个Internet时间服务器:time.windows.com和time.nist.gov大家肯定经常用,反正在我这里是经常时间同步失败,大 ...

  3. 让Windows 时间与Internet 时间服务器同步

    很多新机器开装后发现时间不对,很多人下意识会拿手机或者手表来修改时间.对无法使用Internet的机器来说,手工核对是唯一的解决办法,但是对于连接Internet的机器来说,手工核对时间是完全没有必要 ...

  4. Internet时间自动同步,国内可用的时间服务器

    有没有小伙伴重装系统之后发现,电脑的时间与北京时间不一致,而且每次开机都不会自动同步,其实是系统自带的时间服务器不可用导致的时间问题,下面以win10为例(windows): 1.右击右下角时间 - ...

  5. Windows2012R2,2008R2 Internet时间无法同步成功解决方案

    经测试在2008 R2 2012 R2 上随便用哪个服务器都无法同步时间.如上图所示 解决方法 : Windows2012R2里没有了internet时间,或者Internet时间无法同步成功,都可以 ...

  6. Internet时间自动同步后,计算机系统时间比北京时间不能同步一致

    环景: win10 专业版 2019AD域环境 IPguard管控客户端 问题描述: 与time.windows.com,Internet时间自动同步后,计算机系统时间比北京时间慢一分钟,有时又快一分 ...

  7. 修改Windows的Internet时间服务器使时间同步

    Windows自带的Internet时间服务器会出现不能同步时间的毛病,我选择了上海交通大学网络中心NTP服务器来同步时间,速度很快. 方法:桌面右下角,时钟,点击,调整日期/时间, 日期和时间--- ...

  8. 微软时间源服务器,Windows的Internet时间服务器使时间同步 – 运维那些事

    Windows自带的Internet时间服务器会出现不能同步时间的毛病,我选择了上海交通大学网络中心NTP服务器来同步时间,速度很快. 方法:桌面右下角,时钟,点击,调整日期/时间, 日期和时间--- ...

  9. win10修改时间同步服务器,解决win10时间服务器同步问题|重置win10时间服务配置...

    Windows 任务栏右下角的日期时间,平时可以方便我们查看,帮助我们更好的安排时间.但是有些用户发现Windows时间错误了,调整过好几次,时间还是不对,这篇文章是PE吧给大家带来的使用命令提示符重 ...

最新文章

  1. Python字符串方法用示例解释
  2. golang 开发笔记
  3. oracle锁表导致update,delete,insert出现无反应的问题
  4. ACL 2018 收录论文 | 如何高效提炼有效信息?
  5. 让你的silverlight更炫(一):从自定义控件说起
  6. RabbitMQ学习之队列监控
  7. 基于JAVA+Servlet+JSP+MYSQL的失物招领系统
  8. sql 单引号_三种数据库的 SQL 注入详解
  9. scrapy 处理动态加载,使用phantomjs
  10. SVN如何迁移到Git?
  11. saliency filters精读之permutohedral lattice
  12. Oracle数据库安装教程--Oracle19c DataBase
  13. linux下制作dos启动u盘启动,在Linux系统下创建FreeDOS可启动U盘
  14. 一次编写命令时遇到的问题,Ambiguous method call.both
  15. 研究生必备的文献翻译软件知云文献翻译替代品--Mac monterey
  16. HTML 中几种常见长度单位介绍
  17. css图片动画主要知识点
  18. AOP防止表单重复提交
  19. 适合小白的大白话讲解---Git与Github的区别
  20. java catch中抛出异常_简单了解Java编程中抛出异常的方法

热门文章

  1. Retrofit详解(二)(Retrofit核心流程)
  2. 数据结构与计算机网络,如何把计算机原理,操作系统,数据结构和计算机网络结合起来...
  3. STL源码剖析-第一章STL概论与版本简介
  4. 师夷之技以制夷 吾尝终日而思矣,不如须臾之所学也
  5. TYVJ P1092 麻将
  6. 网关是什么,一文带你快速入门腾讯技术工程
  7. 坚果保龄球题解(c++)
  8. 面试题:垃圾回收机制(GC)
  9. MVVM 和 VUE三要素:响应式、模板引擎、渲染
  10. 汉字区位码、机内码学习笔记