二、通过自定义类来读取、设置系统时间日期//引入名字空间 using System.Runtime.InteropServices //申明 [DllImport("kernel32.dll", EntryPoint="GetLocalTime")] public static extern void GetLocalTime (ref SystemTimeDate lpSystemTime); [DllImport("kernel32.dll", EntryPoint="SetLocalTime")] public static extern int SetLocalTime (ref SystemTimeDate lpSystemTime ); //定义时间日期类 public struct SystemTimeDate { 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 GetTime_Click(object sender, System.EventArgs e) { //将当前时间日期显示在窗体标题上 SystemTimeDate st=new SystemTimeDate(); GetLocalTime(ref st); this .Text="The Date and DateTime is: " ; this .Text=this.Text+st.wYear.ToString()+"年"; this .Text=this.Text+st.wMonth.ToString()+"月"; this .Text=this.Text+st.wDay.ToString()+"日 "; this .Text=this.Text+"星期"+st.wDayOfWeek.ToString(); this .Text=this.Text+" "+st.wHour.ToString()+":"; this .Text=this.Text+st.wHour.ToString()+":"; this .Text=this.Text+st.wMinute.ToString()+":"; this .Text=this.Text+st.wSecond.ToString()+"."; this .Text=this.Text+st.wMilliseconds.ToString(); } //设置系统时间日期 private void GetTime_Click(object sender, System.EventArgs e) { //将DateTimePicker控件的值通过自定义的时间日期类来保存到系统时间 SystemTimeDate st=new SystemTimeDate(); 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(ref st); }
using System.Runtime.InteropServices
//申明
[DllImport("kernel32.dll", EntryPoint="GetLocalTime")]
public static extern void GetLocalTime (ref SystemTimeDate lpSystemTime);
[DllImport("kernel32.dll", EntryPoint="SetLocalTime")]
public static extern int SetLocalTime (ref SystemTimeDate lpSystemTime );
//定义时间日期类
public struct SystemTimeDate
{
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 GetTime_Click(object sender, System.EventArgs e)
{
//将当前时间日期显示在窗体标题上
SystemTimeDate st=new SystemTimeDate();
GetLocalTime(ref st);
this .Text="The Date and DateTime is: " ;
this .Text=this.Text+st.wYear.ToString()+"年";
this .Text=this.Text+st.wMonth.ToString()+"月";
this .Text=this.Text+st.wDay.ToString()+"日 ";
this .Text=this.Text+"星期"+st.wDayOfWeek.ToString();
this .Text=this.Text+" "+st.wHour.ToString()+":";
this .Text=this.Text+st.wHour.ToString()+":";
this .Text=this.Text+st.wMinute.ToString()+":";
this .Text=this.Text+st.wSecond.ToString()+".";
this .Text=this.Text+st.wMilliseconds.ToString();
}
//设置系统时间日期
private void GetTime_Click(object sender, System.EventArgs e)
{
//将DateTimePicker控件的值通过自定义的时间日期类来保存到系统时间
SystemTimeDate st=new SystemTimeDate();
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(ref st);
}