SoFunction
Updated on 2025-03-07

C# to implement a method of modifying system time

The implementation steps for obtaining and modifying system time in C# described in this article are: the system time is taken out from the SystemTime structure and displayed on textBox1. The year, month, day, hour, minute, and second information is obtained from the setDate, setTime control, and stored in the SystemTime structure, and then set it to the time specified by the user using SetLocalTime (ref systemTime). After compiling this code, there will be an easy-to-operate form.

The complete function code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace changesystime
{
 /// <summary>
 /// Summary description of Form1. /// </summary>
 public class Form1 : 
 {
 private  groupBox1;
 private  textBox1;
 private  groupBox2;
 private  button1;
 private  button2;
 private  timer1;
 private  setDate;
 private  setTime;
 private  components;
 [StructLayout()]
 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 system time [DllImport("")]
 public static extern bool SetLocalTime( ref SystemTime sysTime );
 // Used to obtain system time [DllImport("")]
 public static extern void GetLocalTime(ref SystemTime sysTime);
 public Form1()
 {
  //
  // Required for Windows Form Designer Support  //
  InitializeComponent();

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

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

 #region Code generated by Windows Form Designer /// <summary>
 /// Designer supports required methods - do not use code editor to modify /// Contents of this method. /// </summary>
 private void InitializeComponent()
 {
  this.groupBox1 = new ();
  this.textBox1 = new ();
  this.groupBox2 = new ();
   = new ();
   = new ();
  this.button1 = new ();
  this.button2 = new ();
  this.timer1 = new ();
  this.();
  this.();
  (()(this.timer1)).BeginInit();
  ();
  // 
  // groupBox1
  // 
  this.(this.textBox1);
  this. = new (32, 24);
  this. = "groupBox1";
  this. = new (216, 64);
  this. = 0;
  this. = false;
  this. = "System Current Time";
  // 
  // textBox1
  // 
  this. = new (16, 24);
  this. = "textBox1";
  this. = true;
  this. = new (184, 21);
  this. = 1;
  this. = "";
  // 
  // groupBox2
  // 
  this.();
  this.();
  this. = new (32, 112);
  this. = "groupBox2";
  this. = new (216, 64);
  this. = 1;
  this. = false;
  this. = "Time is set to";
  // 
  // setTime
  // 
   = ;
   = new (128, 24);
   = "setTime";
   = true;
   = new (72, 21);
   = 1;
   = false;
  // 
  // setDate
  // 
   = ;
   = new (8, 24);
   = "setDate";
   = new (104, 21);
   = 0;
  // 
  // button1
  // 
  this. = new (40, 200);
  this. = "button1";
  this. = new (64, 32);
  this. = 2;
  this. = "set up";
  this. += new (this.button1_Click);
  // 
  // button2
  // 
  this. = new (168, 200);
  this. = "button2";
  this. = new (64, 32);
  this. = 3;
  this. = "quit";
  this. += new (this.button2_Click);
  // 
  // timer1
  // 
  this. = true;
  this. = this;
  this. += new (this.timer1_Elapsed);
  // 
  // Form1
  // 
   = new (6, 14);
   = new (280, 261);
  (this.button2);
  (this.button1);
  (this.groupBox2);
  (this.groupBox1);
   = "Form1";
   = "Get and set system time";
  this.(false);
  this.(false);
  (()(this.timer1)).EndInit();
  (false);

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

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

 private void timer1_Elapsed(object sender,  e)
 {
  // Clear string on textBox1  ();
  // Create a SystemTime structure to receive the current time of the system  SystemTime systemTime = new SystemTime();
  GetLocalTime(ref systemTime); // Obtain the system time and exist in the SystemTime structure  // Take the system time out of the SystemTime structure and display it on textBox1   += () +"-";
   += () + "-";
   += () + " ";
   += () + ":";
   += () + ":";
   += ();
 // (); 
 }
 private void button1_Click(object sender,  e)
 {
  // Create a SystemTime structure to receive the time set by the user  SystemTime systemTime = new SystemTime();
  // Get year, month, day, hour, minute, second information from setDate, setTime control and save it into the SystemTime structure       = (ushort);
   = (ushort);
   = (ushort);
   = (ushort);
   = (ushort);
   = (ushort);
  // Set the system time to the user specified time  SetLocalTime(ref systemTime);
 }
 }
}