Instance 365 (2) --------- call the system api to modify the system time

Source: Internet
Author: User

I:

Ii. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; using System. runtime. interopServices; namespace SetDate {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} public class SetSystemDateTime // custom class SetSystemDateTime, used to set the system date, to use the DllImportAttribute class (DllImportAttribu A te class can be used to apply attributes to a method. // It is made public by an unmanaged dynamic link library (DLL) as a static entry point. // you need to introduce the namespace: using System. runtime. interopServices; {[DllImportAttribute ("Kernel32.dll")] // use the name of the DLL containing the method to be imported to initialize a new instance of the DllImportAttribute class. Public static extern void GetLocalTime (SystemTime st); // C # The Win32 API must be called to set the system time. The related functions are SetSystemTime (), GetSystemTimer (), setLocalTime (), GetLocalTime (), // This seems to be a function written by VC, which can be called directly in VC ++. // For these two functions, the input parameter must be a struct such as the following, and its member variable type must be ushort. member variables cannot change the sequence. [DllImportAttribute ("Kernel32.dll")] public static extern void SetLocalTime (SystemTime st);} [StructLayoutAttribute (LayoutKind. sequential)] public class SystemTime // custom class SystemTime is used to define the date class {public ushort vYear; // year public ushort vMonth; // month public ushort vDayOfWeek; // public ushort vDay of the week; // public ushort vHour of the day; // public ushort vMinute of the hour; // public ushort vSecond of the hour; // second} private void button1_Click (ob Ject sender, EventArgs e) {this. textBox1.Text = DateTime. now. toString ("F") + // obtain the system time "" + DateTime. now. toString ("dddd");} private void button2_Click (object sender, EventArgs e) {if (MessageBox. show ("are you sure you want to change the current date of the system? ", // Set the current date and time of the system" message prompt ", MessageBoxButtons. OK) = DialogResult. OK) {DateTime Year = this. dateTimePicker1.Value; // obtain the time information SystemTime MySystemTime = new SystemTime (); // create a system time Class Object SetSystemDateTime. getLocalTime (MySystemTime); // obtain the system time MySystemTime. vYear = (ushort) this. dateTimePicker1.Value. year; // set the Year MySystemTime. vMonth = (ushort) this. dateTimePicker1.Value. month; // set the Month MySystemTime. vDay = (ushort) this. dateTimePicker1.Value. day; // set the Day of MySystemTime. vHour = (ushort) this. dateTimePicker2.Value. hour; // set the Hour MySystemTime. vMinute = (ushort) this. dateTimePicker2.Value. minute; // set the Minute to MySystemTime. vSecond = (ushort) this. dateTimePicker2.Value. second; // set the Second SetSystemDateTime. setLocalTime (MySystemTime); // set the system time to button#click (null, null); // execute button to click event }}}}

 

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.