Set Computer System Time

Source: Internet
Author: User

Using system. drawing;
Using system. componentmodel;
Using system. runtime. interopservices;

[Structlayout (layoutkind. Sequential)]
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 wmiliseconds;
}

Public class Win32
{
[Dllimport ("kernel32.dll")]
Public static extern bool setsystemtime (ref systemtime retry IME );
[Dllimport ("kernel32.dll")]
Public static extern void getsystemtime (ref systemtime policime );
}

# Region Time Synchronization
/// <Summary>
/// Set the synchronization time with the server
/// </Summary>
Public void synchronoustime ()
{
Try
{
# Region change computer time

Systemtime required IME = new systemtime ();

Datetime servertime = convert. todatetime ("2005-01-18 ");

Required ime. wyear = convert. touint16 (servertime. year );

Required ime. wmonth = convert. touint16 (servertime. month );

Required ime. wday = convert. touint16 (servertime. Day );

// Disposal at Beijing Time

Int nbeijinghour = servertime. Hour-8;

If (nbeijinghour <= 0)
{
Nbeijinghour + = 24;

Required ime. wday = convert. touint16 (servertime. Day-1 );

Systime. wdayofweek = convert. touint16 (servertime. dayofweek-1 );
}
Else
{
Required ime. wday = convert. touint16 (servertime. Day );

Systime. wdayofweek = convert. touint16 (servertime. dayofweek );
}

Systime. whour = convert. touint16 (nbeijinghour );

Systime. wminute = convert. touint16 (servertime. Minute );

Systime. wsecond = convert. touint16 (servertime. Second );

Systime. wmiliseconds = convert. touint16 (servertime. millisecond );

Win32.setsystemtime (ref systime );

# endregion
}< br> catch
{< br> // if an error occurs, no exception is thrown.
}< BR ># endregion

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.