Change System Time C #

Source: Internet
Author: User
// Two methods, but both need run as admin [structlayout (layoutkind. sequential)] public struct systemtime {public ushort wyear; Public ushort wmonth; Public ushort hour; Public ushort wday; Public ushort whour; Public ushort wminute; Public ushort wsecond; Public ushort hour ;} public static class setosdatetime {[dllimport ("kernel32.dll")] public static extern bool setlocaltime (ref systemtime required IME ); /// <summary> ///// </Summary> /// <Param name = "time"> </param> /// <returns> </ returns> Public static bool setbyapi (string time) {bool flag = false; systemtime required IME = new systemtime (); datetime dt = convert. todatetime (time); required ime. wyear = convert. touint16 (DT. year); required ime. wmonth = convert. touint16 (DT. month); required ime. wday = convert. touint16 (DT. day); required ime. whour = convert. touint16 (DT. hour); your ime. wminute = convert. touint16 (DT. minute); policime. wsecond = convert. touint16 (DT. second); try {flag = setosdatetime. setlocaltime (ref systime);} catch (exception e) {console. writeline ("setsystemdatetime function execution exception" + E. message);} return flag ;} /// <summary> ///// </Summary> /// <Param name = "time"> 02-20-2014 </param> Public static void setbyprocess (string time) {PROCESS p = new process (); p. startinfo. filename = "cmd.exe"; p. startinfo. arguments = "/c Date 02-20-2014"; p. startinfo. useshellexecute = false; p. startinfo. redirectstandardinput = true; p. startinfo. redirectstandardoutput = true; p. startinfo. redirectstandarderror = true; p. startinfo. createnowindow = true; p. start (); // return p. standardoutput. readtoend ();}}

 

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.