Setting system time using C #

Source: Internet
Author: User
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
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);
}


<summary>
Summary description of the Form1.
</summary>
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;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent 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);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
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);
This.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

<summary>
The main entry point for the application.
</summary>
[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 systime = new SystemTime ();

Systime.wyear = Convert.touint16 (DateTime.Now.Year);
Systime.wmonth = Convert.touint16 (DateTime.Now.Month);
The disposal of the
int nbeijinghour = datetime.now.hour-8;
if (nbeijinghour <= 0)
{
Nbeijinghour + 24;
systime.wday= convert.touint16 (datetime.now.day-1);
Systime.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 ();
}


}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.