Use C # to set system time (different from previous articles, is tested)

xiaoxiao2021-03-06  97

Using system.drawing; using system.collections; using system.windows.form; using system.data; using system.runtime.interopservices;

namespace WindowsApplication1 {[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 sysTime); [DllImport ( "Kernel32.dll")] public static extern void GetSystemTime (ref SystemTime sysTime);}

///

/// Form1 summary description. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; / // /// The required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method. /// private void InitializeComponent () {this.button1 = new System.Windows.Forms.Button (); this.textBox1 = new System.Windows.Forms.TextBox (); this.textBox2 = new System. Windows.Forms.TextBox (); this.suspendlayout (); // // button1 // this.button1.location = new system.drawing.point (188, 174); this.button1.name = "button1"; this .Button1.tabindex = 0; this.button1.text = "button1"; this.button1.click = new system.EventHandler (this.button1_click); //// textBox1 // this.textbox1.location = new system. Drawing.point (56, 12); this.textbox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (510, 21); this.textbox1.tabindex = 1; this.TextBox1.text = "textbox1"; // // textBox2 // this.textbox2.location = new system.drawing.point (58, 50); this.textbox2.name = "textbox2"; this.textbox2.size = new system.drawing .Size (506, 21); thi S.TextBox2.TabINDEX = 2; this.textBox2.text = "textbox2"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing. Size (602, 273); this.controls.add (this.TextBox2); this.Controls.add (this.TextBox1); this.Controls.add (this.button1); this.name = "form1"; this. Text = "form1"; this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [STAThread] static void Main () {Application.Run (new Form1 ());} private void button1_Click (object sender, System.EventArgs e) {textBox1.Text = DateTime.Now.ToLongDateString ( DateTime.now.tolongTimeString (); SystemTime SystemTime ();

SYSTIME.WYEAR = Convert.TOTIME.NOW.YEAR); system.wmonth = convert.touint16 (DateTime.now.month); // Disposable Beijing Time Int NbeijingHour = DateTime.Now.Hour - 8; IF (NBeijingHour < = 0) {nbeijingHour = 24; system.wday = convert.touint16 (datetime.now.day - 1); system.wdayofweek = convert.touint16 (DateTime.now.dayofweek - 1);} else {systime.wday = Convert.ToUInt16 (DateTime.Now.Day); sysTime.wDayOfWeek = Convert.ToUInt16 (DateTime.Now.DayOfWeek);} sysTime.wHour = Convert.ToUInt16 (nBeijingHour); sysTime.wMinute = Convert.ToUInt16 (DateTime.Now. Minute); sysTime.wSecond = Convert.ToUInt16 (DateTime.Now.Second); sysTime.wMiliseconds = Convert.ToUInt16 (DateTime.Now.Millisecond); Win32.SetSystemTime (ref sysTime); textBox2.Text = DateTime.Now.ToLongDateString () DateTime.now.tolongTimeString ();

}

转载请注明原文地址:https://www.9cbs.com/read-105404.html

New Post(0)