C implements methods to modify system time

  • 2020-06-23 01:46:50
  • OfStack

In this article, C# gets and modifies the system time as follows: the system time is taken from the SystemTime structure and displayed on textBox1, the year, month, day, hour, minute, and second information is obtained from the setDate,setTime controls, stored in the SystemTime structure, and then set to the user-specified time using SetLocalTime(ref systemTime). This code will be compiled with an easy to operate form.

The complete functional code is as follows:


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace changesystime
{
 /// <summary>
 /// Form1  The summary description of. 
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
 private System.Windows.Forms.GroupBox groupBox1;
 private System.Windows.Forms.TextBox textBox1;
 private System.Windows.Forms.GroupBox groupBox2;
 private System.Windows.Forms.Button button1;
 private System.Windows.Forms.Button button2;
 private System.Timers.Timer timer1;
 private System.Windows.Forms.DateTimePicker setDate;
 private System.Windows.Forms.DateTimePicker setTime;
 private System.ComponentModel.IContainer components;
 [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;
 }
 
 //  Used to set the system time 
 [DllImport("Kernel32.dll")]
 public static extern bool SetLocalTime( ref SystemTime sysTime );
 //  Used to obtain system time 
 [DllImport("Kernel32.dll")]
 public static extern void GetLocalTime(ref SystemTime sysTime);
 public Form1()
 {
  //
  // Windows  Forms designer support required 
  //
  InitializeComponent();

  //
  // TODO:  in  InitializeComponent  Add any constructor code after the call 
  //
 }

 /// <summary>
 ///  Clean up all resources that are in use. 
 /// </summary>
 protected override void Dispose( bool disposing )
 {
  if( disposing )
  {
  if (components != null) 
  {
   components.Dispose();
  }
  }
  base.Dispose( disposing );
 }

 #region Windows  Code generated by the forms designer 
 /// <summary>
 ///  The designer supports the required methods  -  Do not modify using the code editor 
 ///  The contents of this method. 
 /// </summary>
 private void InitializeComponent()
 {
  this.groupBox1 = new System.Windows.Forms.GroupBox();
  this.textBox1 = new System.Windows.Forms.TextBox();
  this.groupBox2 = new System.Windows.Forms.GroupBox();
  this.setTime = new System.Windows.Forms.DateTimePicker();
  this.setDate = new System.Windows.Forms.DateTimePicker();
  this.button1 = new System.Windows.Forms.Button();
  this.button2 = new System.Windows.Forms.Button();
  this.timer1 = new System.Timers.Timer();
  this.groupBox1.SuspendLayout();
  this.groupBox2.SuspendLayout();
  ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
  this.SuspendLayout();
  // 
  // groupBox1
  // 
  this.groupBox1.Controls.Add(this.textBox1);
  this.groupBox1.Location = new System.Drawing.Point(32, 24);
  this.groupBox1.Name = "groupBox1";
  this.groupBox1.Size = new System.Drawing.Size(216, 64);
  this.groupBox1.TabIndex = 0;
  this.groupBox1.TabStop = false;
  this.groupBox1.Text = " System current time ";
  // 
  // textBox1
  // 
  this.textBox1.Location = new System.Drawing.Point(16, 24);
  this.textBox1.Name = "textBox1";
  this.textBox1.ReadOnly = true;
  this.textBox1.Size = new System.Drawing.Size(184, 21);
  this.textBox1.TabIndex = 1;
  this.textBox1.Text = "";
  // 
  // groupBox2
  // 
  this.groupBox2.Controls.Add(this.setTime);
  this.groupBox2.Controls.Add(this.setDate);
  this.groupBox2.Location = new System.Drawing.Point(32, 112);
  this.groupBox2.Name = "groupBox2";
  this.groupBox2.Size = new System.Drawing.Size(216, 64);
  this.groupBox2.TabIndex = 1;
  this.groupBox2.TabStop = false;
  this.groupBox2.Text = " Time set to ";
  // 
  // setTime
  // 
  this.setTime.Format = System.Windows.Forms.DateTimePickerFormat.Time;
  this.setTime.Location = new System.Drawing.Point(128, 24);
  this.setTime.Name = "setTime";
  this.setTime.ShowUpDown = true;
  this.setTime.Size = new System.Drawing.Size(72, 21);
  this.setTime.TabIndex = 1;
  this.setTime.TabStop = false;
  // 
  // setDate
  // 
  this.setDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
  this.setDate.Location = new System.Drawing.Point(8, 24);
  this.setDate.Name = "setDate";
  this.setDate.Size = new System.Drawing.Size(104, 21);
  this.setDate.TabIndex = 0;
  // 
  // button1
  // 
  this.button1.Location = new System.Drawing.Point(40, 200);
  this.button1.Name = "button1";
  this.button1.Size = new System.Drawing.Size(64, 32);
  this.button1.TabIndex = 2;
  this.button1.Text = " Set up the ";
  this.button1.Click += new System.EventHandler(this.button1_Click);
  // 
  // button2
  // 
  this.button2.Location = new System.Drawing.Point(168, 200);
  this.button2.Name = "button2";
  this.button2.Size = new System.Drawing.Size(64, 32);
  this.button2.TabIndex = 3;
  this.button2.Text = " exit ";
  this.button2.Click += new System.EventHandler(this.button2_Click);
  // 
  // timer1
  // 
  this.timer1.Enabled = true;
  this.timer1.SynchronizingObject = this;
  this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
  // 
  // Form1
  // 
  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  this.ClientSize = new System.Drawing.Size(280, 261);
  this.Controls.Add(this.button2);
  this.Controls.Add(this.button1);
  this.Controls.Add(this.groupBox2);
  this.Controls.Add(this.groupBox1);
  this.Name = "Form1";
  this.Text = " Gets and sets the system time ";
  this.groupBox1.ResumeLayout(false);
  this.groupBox2.ResumeLayout(false);
  ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
  this.ResumeLayout(false);

 }
 #endregion
 /// <summary>
 ///  The main entry point to the application. 
 /// </summary>
 [STAThread]
 static void Main() 
 {
  Application.Run(new Form1());
 }

 private void button2_Click(object sender, System.EventArgs e)
 {
  this.Close(); //  Close the current form 
 }

 private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
  //  remove textBox1 String on 
  textBox1.Clear();
  //  create SystemTime A structure used to receive the current time of the system 
  SystemTime systemTime = new SystemTime();
  GetLocalTime(ref systemTime); //  Get the time and existence of the system SystemTime In the structure 
  //  Take the time from the system  SystemTime  The structure is taken out and displayed in textBox1 on 
  textBox1.Text += systemTime.wYear.ToString() +"-";
  textBox1.Text += systemTime.wMonth.ToString() + "-";
  textBox1.Text += systemTime.wDay.ToString() + " ";
  textBox1.Text += systemTime.wHour.ToString() + ":";
  textBox1.Text += systemTime.wMinute.ToString() + ":";
  textBox1.Text += systemTime.wSecond.ToString();
 // textBox1.Refresh(); 
 }
 private void button1_Click(object sender, System.EventArgs e)
 {
  //  create SystemTime Structure used to receive the time set by the user 
  SystemTime systemTime = new SystemTime();
  //  from setDate,setTime Control to get year, month, day, hour, minute, second information, and save SystemTime In the structure 
      systemTime.wYear = (ushort)setDate.Value.Year;
  systemTime.wMonth = (ushort)setDate.Value.Month;
  systemTime.wDay = (ushort)setDate.Value.Day;
  systemTime.wHour = (ushort)setTime.Value.Hour;
  systemTime.wMinute = (ushort)setTime.Value.Minute;
  systemTime.wSecond = (ushort)setTime.Value.Second;
  //  Sets the system time to the time specified by the user 
  SetLocalTime(ref systemTime);
 }
 }
}

Related articles: