使用c#实现网络时间同步功能

使用c#实现网络时间同步功能

ID:25788606

大小:31.65 KB

页数:5页

时间:2018-11-22

使用c#实现网络时间同步功能_第1页
使用c#实现网络时间同步功能_第2页
使用c#实现网络时间同步功能_第3页
使用c#实现网络时间同步功能_第4页
使用c#实现网络时间同步功能_第5页
资源描述:

《使用c#实现网络时间同步功能》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、杭州java软件培训www.zjbenet.com在windowsserver系列的操作性中,存在一个同步时间的服务器,可惜很多服务器都禁用了这个功能。在日常自己的电脑上也会出现时间和标准时间不一致的问题。既然自己是学编程的,那么就自己动手丰衣足食吧。下载是通过获取网络标准时间的源码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Text

2、.RegularExpressions;usingSystem.Runtime.InteropServices;usingSystem.Runtime;///

///网络时间///publicclassNetTime{//////获取标准北京时间,读取http://www.beijing-time.org/time.asp//////返回网络时间publicDateTimeGetBeijingTime(){DateTimedt;WebRequestwrt=null;W

3、ebResponsewrp=null;try{wrt=WebRequest.Create("http://www.beijing-time.org/time.asp");wrp=wrt.GetResponse();stringhtml=string.Empty;using(Streamstream=wrp.GetResponseStream())杭州网络营销培训www.hzbeidaqingniao.com杭州java软件培训www.zjbenet.com{using(StreamReadersr=newStreamReader(stream,Encoding.UTF8)){h

4、tml=sr.ReadToEnd();}}string[]tempArray=html.Split(';');for(inti=0;i

5、ngminite=tempArray[6].Split('=')[1];stringsecond=tempArray[7].Split('=')[1];dt=DateTime.Parse(year+"-"+month+"-"+day+""+hour+":"+minite+":"+second);}catch(WebException){returnDateTime.Parse("2011-1-1");}catch(Exception){returnDateTime.Parse("2011-1-1");}finally{if(wrp!=null)wrp.Close();if(wr

6、t!=null)wrt.Abort();}returndt;}}杭州网络营销培训www.hzbeidaqingniao.com杭州java软件培训www.zjbenet.com获取网络时间,返回一个DateTime对象,然后传给设置系统时间的方法,修改系统时间。下面是设置系统时间的代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Tex

7、t.RegularExpressions;usingSystem.Runtime.InteropServices;usingSystem.Runtime;///

///更新系统时间///publicclassUpdateTime{//设置系统时间的API函数[DllImport("kernel32.dll")]privatestaticexternboolSetLocalTime(refSYSTEMTIMEtime);[StructLayout(Layou

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。