Example 365 (2)---------call system API to modify system time

Source: Internet
Author: User

One:

Two: Code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;namespacesetdate{ Public Partial classFrm_main:form { PublicFrm_main () {InitializeComponent (); }         Public classSetsystemdatetime//custom class Setsystemdatetime, used to set the system date, in order to use the DllImportAttribute class (The DllImportAttribute class refers to the ability to apply an attribute to a method,//and is exposed as a static entry point by an unmanaged dynamic-link library (DLL).//namespaces need to be introduced: using System.Runtime.InteropServices;{[DllImportAttribute ("Kernel32.dll")]//Initializes a new instance of the DllImportAttribute class with the name of the DLL that contains the method to be imported.              Public Static extern voidGetlocaltime (SystemTime St);//C # To set the system time must call the Win32 API, where the related function is SetSystemTime (), Getsystemtimer (), Setlocaltime (), Getlocaltime (),//this seems to be a VC write function, in VC + + can be directly called. //for these two functions, the input parameter must be a struct of the following, whose member variable type must be ushort, and the member variable cannot be changed in order. [DllImportAttribute ("Kernel32.dll")]             Public Static extern voidSetlocaltime (SystemTime St); } [StructLayoutAttribute (layoutkind.sequential)] Public classSystemTime//Custom class SystemTime for defining date classes        {             Public ushortVyear;//years             Public ushortVmonth;//Month             Public ushortVdayofweek;//Week             Public ushortVDay;//Day             Public ushortVhour;//hours             Public ushortVminute;//points             Public ushortVsecond;//seconds        }        Private voidButton1_Click (Objectsender, EventArgs e) {             This. TextBox1.Text = DateTime.Now.ToString ("F") +//get the system time                "  "+ DateTime.Now.ToString ("dddd"); }        Private voidButton2_Click (Objectsender, EventArgs e) {            if(MessageBox.Show ("Are you sure you want to change the system's current date? ",//set the system current date time                "Information Tips", messageboxbuttons.ok) = =DialogResult.OK) {DateTime year= This. Datetimepicker1.value;//Get time InformationSystemTime MySystemTime=NewSystemTime ();//creating objects for the system time classSetsystemdatetime.getlocaltime (mysystemtime);//get the system timeMysystemtime.vyear= (ushort) This. dateTimePicker1.Value.Year;//Set YearMysystemtime.vmonth = (ushort) This. DateTimePicker1.Value.Month;//Set MonthMysystemtime.vday = (ushort) This. DateTimePicker1.Value.Day;//Set DayMysystemtime.vhour = (ushort) This. DateTimePicker2.Value.Hour;//Set HoursMysystemtime.vminute = (ushort) This. DateTimePicker2.Value.Minute;//Set pointsMysystemtime.vsecond = (ushort) This. DateTimePicker2.Value.Second;//Set secondsSetsystemdatetime.setlocaltime (mysystemtime);//Setting the system timeButton1_Click (NULL,NULL);//Perform a button click event            }        }    }}

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.