c#获取当前系统时间

public class LibWrapDateTime
{
    [DllImportAttribute("Kernel32.dll")]
    public static extern void GetLocalTime(SystemTime st);
    [DllImportAttribute("Kernel32.dll")]
    public static extern void SetLocalTime(SystemTime st);
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class SystemTime
{
    public ushort wYear;
    public ushort wMonth;
    public ushort wDayOfWeek;
    public ushort wDay;
    public ushort wHour;
    public ushort wMinute;
    public ushort wSecond;
    public ushort wMilliseconds;
}
private void button1_Click(object sender, System.EventArgs e)
{ //获取当前系统日期时间			
    DateTimePicker OlddateTimePicker = new DateTimePicker();
    this.textBox1.Text = OlddateTimePicker.Value.ToLongDateString().ToString();
    this.textBox1.Text += OlddateTimePicker.Value.ToLongTimeString().ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{ //设置系统当前日期时间
    if(MessageBox.Show("您真的确定更改系统当前日期时间吗?", "信息提示", MessageBoxButtons.OK) == DialogResult.OK)
    {
        DateTime Year = this.dateTimePicker1.Value;
        SystemTime MySystemTime = new SystemTime();
        LibWrapDateTime.GetLocalTime(MySystemTime);
        MySystemTime.wYear = (ushort) this.dateTimePicker1.Value.Year;
        MySystemTime.wMonth = (ushort) this.dateTimePicker1.Value.Month;
        MySystemTime.wDay = (ushort) this.dateTimePicker1.Value.Day;
        MySystemTime.wHour = (ushort) this.dateTimePicker2.Value.Hour;
        MySystemTime.wMinute = (ushort) this.dateTimePicker2.Value.Minute;
        MySystemTime.wSecond = (ushort) this.dateTimePicker2.Value.Second;
        LibWrapDateTime.SetLocalTime(MySystemTime);
        button1_Click(null, null);
    }
}
posted @ 2020-11-26 09:13  少年。  阅读(961)  评论(0编辑  收藏  举报