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 }}}}