
在程序中有时需要系统时间准确,为了方便修改时间,做了个时间修改的,代码如下
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
namespace
Qualification

...
{
public partial class FormDateSet : Form

...{
public FormDateSet()

...{
InitializeComponent();

}
//调用Kernel32.DLL
[DllImport("Kernel32.dll")]
public static extern void GetLocalTime(SystemTime st);
[DllImport("Kernel32.dll")]
public static extern void SetLocalTime(SystemTime st);


[StructLayout(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 FormDateSet_Load(object sender, EventArgs e)

...{
}
//取得当前系统时间
private void timer1_Tick(object sender, EventArgs e)

...{
SystemTime st = new SystemTime();
GetLocalTime(st);
this.textBox1.Text = st.wYear.ToString()+"-";
this.textBox1.Text = this.textBox1.Text + st.wMonth.ToString() + "-";
this.textBox1.Text = this.textBox1.Text + st.wDay.ToString() + " ";
this.textBox1.Text = this.textBox1.Text + st.Whour.ToString() + ":" + st.wMinute.ToString() + ":" + st.wSecond.ToString();
}

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)

...{
}
//校对系统时间
private void Btn_set_Click(object sender, EventArgs e)

...{
try

...{
SystemTime st = new SystemTime();
st.wYear = (ushort)this.dateTimePicker1.Value.Year;
st.wMonth = (ushort)this.dateTimePicker1.Value.Month;
st.wDay = (ushort)this.dateTimePicker1.Value.Day;
st.Whour = (ushort)this.dateTimePicker1.Value.Hour;
st.wMinute = (ushort)this.dateTimePicker1.Value.Minute;
st.wSecond = (ushort)this.dateTimePicker1.Value.Second;
SetLocalTime(st);
MessageBox.Show("系统时间设置成功!", "系统时间设置成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch

...{
MessageBox.Show("系统时间设置失败!", "设置失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally

...{}
}
}
}