C # synchronous Network Time

Source: Internet
Author: User

The system time of the customer's machine often goes wrong, causing the software to be unable to be used normally, so a small function of synchronizing network time was added later. It is easy to implement, but it is very useful. This small function is to first obtain the network time and then change the system time to the time obtained from the network. The specific implementation is as follows: Get the Network Time: [csharp] using System; using System. collections. generic; using System. linq; using System. text; using System. IO; using System. net; using System. net. sockets; using System. text. regularExpressions; using System. runtime. interopServices; using System. runtime; // <summary> // Network Time // </summary> public class NetTime {// <summary> // obtain the standard Beijing time, read http://www.beijing-time.org/time.asp // /</Summary> // <returns> returns the network time </returns> public DateTime GetBeijingTime () {DateTime dt; WebRequest wrt = null; WebResponse wrp = null; try {wrt = WebRequest. create ("http://www.beijing-time.org/time.asp"); wrp = wrt. getResponse (); string html = string. empty; using (Stream stream = wrp. getResponseStream () {using (StreamReader sr = new StreamReader (stream, Encoding. UTF8) {html = sr. readToEnd () ;}} String [] tempArray = html. split (';'); for (int I = 0; I <tempArray. length; I ++) {tempArray [I] = tempArray [I]. replace ("\ r \ n", "");} string year = tempArray [1]. split ('=') [1]; string month = tempArray [2]. split ('=') [1]; string day = tempArray [3]. split ('=') [1]; string hour = tempArray [5]. split ('=') [1]; string minite = tempArray [6]. split ('=') [1]; string second = tempArray [7]. split ('=') [1]; dt = DateTime. parse (year + "-" + month + "-" + day + "" + hour + ":" + minite + ":" + second);} catch (WebException) {return DateTime. parse ("2011-1-1");} catch (Exception) {return DateTime. parse ("2011-1-1");} finally {if (wrp! = Null) wrp. Close (); if (wrt! = Null) wrt. Abort ();} return dt ;}} get the network time, return a DateTime object, and pass it to the system time setting method to modify the system time. Synchronization System Time: [csharp] using System; using System. collections. generic; using System. linq; using System. text; using System. IO; using System. net; using System. net. sockets; using System. text. regularExpressions; using System. runtime. interopServices; using System. runtime; /// <summary> /// update system time /// </summary> public class UpdateTime {// API function for setting system time [DllImport ("kernel32.dll")] private static extern bool SetL OcalTime (ref SYSTEMTIME time); [StructLayout (LayoutKind. sequential)] private struct SYSTEMTIME {public short year; public short month; public short dayOfWeek; public short day; public short hour; public short minute; public short second; public short milliseconds ;} /// <summary> /// set the system time /// </summary> /// <param name = "dt"> time to be set </param> // /<returns> returns the system time setting status, true indicates success, false indicates failure </returns> publ Ic static bool SetDate (DateTime dt) {SYSTEMTIME st; st. year = (short) dt. year; st. month = (short) dt. month; st. dayOfWeek = (short) dt. dayOfWeek; st. day = (short) dt. day; st. hour = (short) dt. hour; st. minute = (short) dt. minute; st. second = (short) dt. second; st. milliseconds = (short) dt. millisecond; bool rt = SetLocalTime (ref st); return rt ;}} two methods are written in two classes respectively. You only need to instantiate two objects on the client and then call the methods in sequence, simple and practical. PS: The administrator privilege is required to modify the system time in Win8. The next blog introduces how to run the program with the administrator privilege by default. Please wait!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.