Change System Time in C # & get notification of system time change

Source: Internet
Author: User

1. Change System Time

C # system. the datetime class encapsulates the date and time, which is convenient for time conversion and processing, but I have not found any member in it that can be used to modify the system time. Friends who have used VC and VB may know that we can call Win32 API setlocaltime to change the system time. It seems that C # can only do this. Setlocaltime requires a systemtime structure pointer as a parameter, which is not difficult. We can quickly define this structure in C #, but the problem is, I also want to "enjoy ".. NET Framework system. what should I do if datetime is convenient in time conversion and processing? You only need to do it yourself. Compile a member function for systemtime in C # to convert the function to system. datetime. See the following code:

First, I created a new C # file to facilitate future calls, put setlocaltimeapi in a class named WIN32API, and put the class in my own namespace, avoid Name Conflict. In the future, I can add frequently-used APIs to this class and namespace to construct my own C # Win32 API function library J.

// Forproc_win32.cs

// Declare common Win32 API functions and structures

Using system;

Using system. runtime. interopservices;

 

Namespace farproc. Win32

{

/// <Summary>

///

/// </Summary>

Public structsystemtime

{

Public ushort wyear;

Public ushort wmonth;

Public ushortwdayofweek;

Public ushort wday;

Public ushort whour;

Public ushortwminute;

Public ushortwsecond;

Public ushortwmilliseconds;

/// <Summary>

/// Convert from system. datetime.

/// </Summary>

/// <Param name = "time"> system. datetime type time. </Param>

Public void fromdatetime (datetimetime)

{

Wyear = (ushort) Time. Year;

Wmonth = (ushort) Time. month;

Wdayofweek = (ushort) Time. dayofweek;

Wday = (ushort) Time. Day;

Whour = (ushort) Time. hour;

Wminute = (ushort) Time. minute;

Wsecond = (ushort) time. Second;

Wmilliseconds = (ushort) Time. millisecond;

}

/// <Summary>

/// Convert to system. datetime type.

/// </Summary>

/// <Returns> </returns>

Public datetime todatetime ()

{

Return newdatetime (wyear, wmonth, wday, whour, wminute, wsecond, wmilliseconds );

}

/// <Summary>

/// Static method. Convert to system. datetime type.

/// </Summary>

/// <Paramname = "time"> systemtime type time. </Param>

/// <Returns> </returns>

Public staticdatetime todatetime (systemtime time)

{

Return time. todatetime ();

}

}

Public class WIN32API

{

[Dllimport ("kernel32.dll")]

Public static extern bool setlocaltime (ref systemtime time );

[Dllimport ("kernel32.dll")]

Public static extern voidgetlocaltime (ref systemtime time );

}

}

It is called in the winform program below. Create a C # winform program, add the button button1 on the form, and add the following code:

Private void button#click (Object sender, system. eventargs E)

{

// Obtain the current system time

Datetime T = datetime. now;

// Add a week to the current time

T = T. adddays (7 );

// Convert system. datetime to systemtime

Systemtime ST = new systemtime ();

St. fromdatetime (t );

// Call Win32 API to set the system time

WIN32API. setlocaltime (ref st );

// Display the current time

MessageBox. Show (datetime. Now. tostring ());

}

Then you can run the program to see the effect.

2. Get the system time change notification

Another problem is that if the system time is changed manually, my program wants to get a notification and handle it accordingly. What should I do? It seems very difficult. This seems easier than the previous question.

There is a systemevents class in the Microsoft. Win32 namespace. We only need to customize a static event timechanged !!

The code snippet is as follows:

Private void form1_load (Object sender, system. eventargs E)

{

// Custom events

Systemevents. timechanged + = new eventhandler (systemevents_timechanged );

}

// Event processing functions

Private voidsystemevents_timechanged (Object sender, eventargs E)

{

MessageBox. Show ("the system time has been changed. The current time is:" + datetime. Now. tostring ());

}

Note that this event is invalid in the console program, probably because it depends on the window and window message.

 

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.