1. 程式人生 > >C#修改系統時間

C#修改系統時間

rgs 用戶 sys osi using ace draw com click

  1 using System;
  2 using System.Drawing;
  3 using System.Collections;
  4 using System.ComponentModel;
  5 using System.Windows.Forms;
  6 using System.Data;
  7 using System.Runtime.InteropServices;
  8 namespace 獲取和設置系統時間
  9 {
 10     /// <summary>
 11     /// Form1 的摘要說明。
 12     /// </summary>
13 public class Form1 : System.Windows.Forms.Form 14 { 15 private System.Windows.Forms.GroupBox groupBox1; 16 private System.Windows.Forms.TextBox textBox1; 17 private System.Windows.Forms.GroupBox groupBox2; 18 private System.Windows.Forms.Button button1;
19 private System.Windows.Forms.Button button2; 20 private System.Timers.Timer timer1; 21 private System.Windows.Forms.DateTimePicker setDate; 22 private System.Windows.Forms.DateTimePicker setTime; 23 private System.ComponentModel.IContainer components; 24
[StructLayout(LayoutKind.Sequential)] 25 public struct SystemTime 26 { 27 public ushort wYear; 28 public ushort wMonth; 29 public ushort wDayOfWeek; 30 public ushort wDay; 31 public ushort wHour; 32 public ushort wMinute; 33 public ushort wSecond; 34 public ushort wMiliseconds; 35 } 36 37 // 用於設置系統時間 38 [DllImport("Kernel32.dll")] 39 public static extern bool SetLocalTime( ref SystemTime sysTime ); 40 // 用於獲得系統時間 41 [DllImport("Kernel32.dll")] 42 public static extern void GetLocalTime(ref SystemTime sysTime); 43 public Form1() 44 { 45 // 46 // Windows 窗體設計器支持所必需的 47 // 48 InitializeComponent(); 49 50 // 51 // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼 52 // 53 } 54 55 /// <summary> 56 /// 清理所有正在使用的資源。 57 /// </summary> 58 protected override void Dispose( bool disposing ) 59 { 60 if( disposing ) 61 { 62 if (components != null) 63 { 64 components.Dispose(); 65 } 66 } 67 base.Dispose( disposing ); 68 } 69 70 #region Windows 窗體設計器生成的代碼 71 /// <summary> 72 /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 73 /// 此方法的內容。 74 /// </summary> 75 private void InitializeComponent() 76 { 77 this.groupBox1 = new System.Windows.Forms.GroupBox(); 78 this.textBox1 = new System.Windows.Forms.TextBox(); 79 this.groupBox2 = new System.Windows.Forms.GroupBox(); 80 this.setTime = new System.Windows.Forms.DateTimePicker(); 81 this.setDate = new System.Windows.Forms.DateTimePicker(); 82 this.button1 = new System.Windows.Forms.Button(); 83 this.button2 = new System.Windows.Forms.Button(); 84 this.timer1 = new System.Timers.Timer(); 85 this.groupBox1.SuspendLayout(); 86 this.groupBox2.SuspendLayout(); 87 ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit(); 88 this.SuspendLayout(); 89 // 90 // groupBox1 91 // 92 this.groupBox1.Controls.Add(this.textBox1); 93 this.groupBox1.Location = new System.Drawing.Point(32, 24); 94 this.groupBox1.Name = "groupBox1"; 95 this.groupBox1.Size = new System.Drawing.Size(216, 64); 96 this.groupBox1.TabIndex = 0; 97 this.groupBox1.TabStop = false; 98 this.groupBox1.Text = "系統當前時間"; 99 // 100 // textBox1 101 // 102 this.textBox1.Location = new System.Drawing.Point(16, 24); 103 this.textBox1.Name = "textBox1"; 104 this.textBox1.ReadOnly = true; 105 this.textBox1.Size = new System.Drawing.Size(184, 21); 106 this.textBox1.TabIndex = 1; 107 this.textBox1.Text = ""; 108 // 109 // groupBox2 110 // 111 this.groupBox2.Controls.Add(this.setTime); 112 this.groupBox2.Controls.Add(this.setDate); 113 this.groupBox2.Location = new System.Drawing.Point(32, 112); 114 this.groupBox2.Name = "groupBox2"; 115 this.groupBox2.Size = new System.Drawing.Size(216, 64); 116 this.groupBox2.TabIndex = 1; 117 this.groupBox2.TabStop = false; 118 this.groupBox2.Text = "時間設置為"; 119 // 120 // setTime 121 // 122 this.setTime.Format = System.Windows.Forms.DateTimePickerFormat.Time; 123 this.setTime.Location = new System.Drawing.Point(128, 24); 124 this.setTime.Name = "setTime"; 125 this.setTime.ShowUpDown = true; 126 this.setTime.Size = new System.Drawing.Size(72, 21); 127 this.setTime.TabIndex = 1; 128 this.setTime.TabStop = false; 129 // 130 // setDate 131 // 132 this.setDate.Format = System.Windows.Forms.DateTimePickerFormat.Short; 133 this.setDate.Location = new System.Drawing.Point(8, 24); 134 this.setDate.Name = "setDate"; 135 this.setDate.Size = new System.Drawing.Size(104, 21); 136 this.setDate.TabIndex = 0; 137 // 138 // button1 139 // 140 this.button1.Location = new System.Drawing.Point(40, 200); 141 this.button1.Name = "button1"; 142 this.button1.Size = new System.Drawing.Size(64, 32); 143 this.button1.TabIndex = 2; 144 this.button1.Text = "設置"; 145 this.button1.Click += new System.EventHandler(this.button1_Click); 146 // 147 // button2 148 // 149 this.button2.Location = new System.Drawing.Point(168, 200); 150 this.button2.Name = "button2"; 151 this.button2.Size = new System.Drawing.Size(64, 32); 152 this.button2.TabIndex = 3; 153 this.button2.Text = "退出"; 154 this.button2.Click += new System.EventHandler(this.button2_Click); 155 // 156 // timer1 157 // 158 this.timer1.Enabled = true; 159 this.timer1.SynchronizingObject = this; 160 this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed); 161 // 162 // Form1 163 // 164 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 165 this.ClientSize = new System.Drawing.Size(280, 261); 166 this.Controls.Add(this.button2); 167 this.Controls.Add(this.button1); 168 this.Controls.Add(this.groupBox2); 169 this.Controls.Add(this.groupBox1); 170 this.Name = "Form1"; 171 this.Text = "獲取和設置系統時間"; 172 this.groupBox1.ResumeLayout(false); 173 this.groupBox2.ResumeLayout(false); 174 ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit(); 175 this.ResumeLayout(false); 176 177 } 178 #endregion 179 /// <summary> 180 /// 應用程序的主入口點。 181 /// </summary> 182 [STAThread] 183 static void Main() 184 { 185 Application.Run(new Form1()); 186 } 187 188 private void button2_Click(object sender, System.EventArgs e) 189 { 190 this.Close(); // 關閉當前窗體 191 } 192 193 private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 194 { 195 // 清除textBox1上的字符串 196 textBox1.Clear(); 197 // 創建SystemTime結構體,用於接收系統當前時間 198 SystemTime systemTime = new SystemTime(); 199 GetLocalTime(ref systemTime); // 獲得系統的時間並存在SystemTime結構體中 200 // 將系統的時間從 SystemTime 結構體中中取出,並顯示在textBox1上 201 textBox1.Text += systemTime.wYear.ToString() +"-"; 202 textBox1.Text += systemTime.wMonth.ToString() + "-"; 203 textBox1.Text += systemTime.wDay.ToString() + " "; 204 textBox1.Text += systemTime.wHour.ToString() + ":"; 205 textBox1.Text += systemTime.wMinute.ToString() + ":"; 206 textBox1.Text += systemTime.wSecond.ToString(); 207 // textBox1.Refresh(); 208 } 209 private void button1_Click(object sender, System.EventArgs e) 210 { 211 // 創建SystemTime結構體,用於接收用戶設置的時間 212 SystemTime systemTime = new SystemTime(); 213 // 從setDate,setTime控件中獲取年,月,日,小時,分鐘,秒信息,存入SystemTime結構體中 214 systemTime.wYear = (ushort)setDate.Value.Year; 215 systemTime.wMonth = (ushort)setDate.Value.Month; 216 systemTime.wDay = (ushort)setDate.Value.Day; 217 systemTime.wHour = (ushort)setTime.Value.Hour; 218 systemTime.wMinute = (ushort)setTime.Value.Minute; 219 systemTime.wSecond = (ushort)setTime.Value.Second; 220 // 將系統的時間設置為用戶指定的時間 221 SetLocalTime(ref systemTime); 222 } 223 } 224 }

C#修改系統時間