A description of this document
This document mainly in order to solve the actual development, the server and client computer time is not equal problem, pure dry goods, the actual project this time is not synchronized many many, time is not equal, when the local data submitted to the server, the server to see the actual time and I write the database time difference is great, It affects actual business operations and determines the accuracy of the business, so it is necessary to set the time of the device or computer to provide accurate time nodes for uploads.
It is welcome to share the information of the original person, but it is forbidden to use the document directly for commercial profit.
I have been a few years ago on the road of programming, has been committed to collecting and summarizing the use of the framework and general-purpose library, whether it is Microsoft's own or third-party as long as the actual project is good and can solve the actual problem that will be collected, write good articles and others to share, so that they learned, others can learn knowledge, A porter who needs knowledge in today's society.
Email:[email protected]
This article address: http://www.cnblogs.com/wohexiaocai/p/5721906.html
1. Basic Introduction
In actual development, we need to test the computer time is not accurate time of the business impact on the need to use this class, to help us set the computer the correct point in time.
in general, we are setting the user's computer time, not the server time Oh , the server time can be unified set with NTP, Science: http://baike.baidu.com/link?url= Bq6u-qi2ufjsqmxabjxnleme69cefpfrjmax4hflvhgrijp1v6hi2yzpyfabcgnjgnxp-yykppraptgru3czna
2. How to get the right time to get to the right time, the general will be our server time to get to it, the server time is generally networked, time is more accurate, the actual project we are through the call server interface to get to the current time, this is not difficult
1 #regionpublic static string GetResponse (string url)2 /// <summary>3 ///get a Web page4 /// </summary>5 /// <param name= "url" >Address</param>6 /// <returns>String return value</returns>7 Public Static stringGetResponse (stringURL)8 {9 stringresult =NULL;TenWebResponse WebResponse =NULL; OneStreamReader StreamReader =NULL; A Try - { -HttpWebRequest HttpWebRequest =(HttpWebRequest) webrequest.create (URL); theHttpwebrequest.method ="GET"; -WebResponse =httpwebrequest.getresponse (); -StreamReader =NewStreamReader (WebResponse.GetResponseStream (), Encoding.UTF8); -result =streamreader.readtoend (); + } - Catch + { A //Handle Error at } - finally - { - if(StreamReader! =NULL) - { - streamreader.close (); in } - if(WebResponse! =NULL) to { + webresponse.close (); - } the } * returnresult; $ }Panax Notoginseng #endregion
Get Time3. Set the computer time to get time to set up, the general Windows system is the DOS command to set the time of the computer, so here also use C # DOS to set the 3.1 set date
Sometimes only need to set the date of the computer
public static void Setlocaldate (int year, int month, Int. day)
3.2 Setting the time
Sometimes you just need to set the computer's time division seconds
public static void Setlocaltime (int hour, int min, int sec)
3.3 Set the day of the month and seconds
Sometimes you just need to set the computer's time division seconds
public static void Setlocaldatetime (DateTime time)
4. Actual effects
After Win7, XP, Windows Server and other tests can be set to normal computer time, please you can use normal, do not combat how dare to take out direct use it?
5. Source code Download
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Diagnostics;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 namespaceConsoleApplication19 {Ten Public classDatetimehelper One { A /// <summary> - ///set the date of the local computer - /// </summary> the /// <param name= "year" ></param> - /// <param name= "month" ></param> - /// <param name= "Day" ></param> - Public Static voidSetlocaldate (intYearintMonthintDay ) + { - //instance a process class, starting an independent +Process p =NewProcess (); A //The Process class has a StartInfo property at //Set Program name -p.StartInfo.FileName ="Cmd.exe"; - //set the program execution parameter "/C" to exit immediately after executing the command -P.startinfo.arguments =string. Format ("/ C Date {0}-{1}-{2}", year, month, day); - //turn off the use of the shell -P.startinfo.useshellexecute =false; in //REDIRECT standard input -P.startinfo.redirectstandardinput =true; toP.startinfo.redirectstandardoutput =true; + //REDIRECT error output -P.startinfo.redirectstandarderror =true; the //set not to show Doc window *P.startinfo.createnowindow =true; $ //StartPanax Notoginseng P.start (); - //get command execution results from output stream the p.standardoutput.readtoend (); + } A the /// <summary> + ///set the local computer's time-division seconds - /// </summary> $ /// <param name= "Hour" ></param> $ /// <param name= "min" ></param> - /// <param name= "SEC" ></param> - Public Static voidSetlocaltime (intHourintMinintsec) the { - //instance a process class, starting an independentWuyiProcess p =NewProcess (); the //The Process class has a StartInfo property - //Set Program name Wup.StartInfo.FileName ="Cmd.exe"; - //set the program execution parameter "/C" to exit immediately after executing the command AboutP.startinfo.arguments =string. Format ("/ C time {0}:{1}:{2}", Hour, Min, sec); $ //turn off the use of the shell -P.startinfo.useshellexecute =false; - //REDIRECT standard input -P.startinfo.redirectstandardinput =true; AP.startinfo.redirectstandardoutput =true; + //REDIRECT error output theP.startinfo.redirectstandarderror =true; - //set not to show Doc window $P.startinfo.createnowindow =true; the //Start the P.start (); the //get command execution results from output stream the p.standardoutput.readtoend (); - } in the /// <summary> the ///set the date and time of the computer About /// </summary> the /// <param name= "Time" ></param> the Public Static voidSetlocaldatetime (DateTime time) the { + Setlocaldate (time. Year, time. Month, time. Day); - Setlocaltime (time. Hour, time. Minute, time. Second); the }Bayi } the}
"C # Public help class" Datetimehelper set the PC local time, the actual development is very necessary