/// <summary> /// 设置系统时间 /// </summary> public class SetSTime { [DllImport("Kernel32.dll")] public static extern bool SetSystemTime(ref SystemTime sysTime); [DllImport("Kernel32.dll")] public static extern void GetSystemTime(ref SystemTime sysTime); /// <summary> /// 设置系统时间方法 /// </summary> /// <param name="dt"></param> public static void SetSysTime(DateTime dt) { //不知道什么原因,必须先加八个小时,才能是准确时间。估计是时区问题 DateTime dateTimePicker1 = dt.AddHours(-8); SystemTime MySystemTime = new SystemTime(); MySystemTime.vYear = (ushort)dateTimePicker1.Year; MySystemTime.vMonth = (ushort)dateTimePicker1.Month; MySystemTime.vDay = (ushort)dateTimePicker1.Day; MySystemTime.vHour = (ushort)dateTimePicker1.Hour; MySystemTime.vMinute = (ushort)dateTimePicker1.Minute; MySystemTime.vSecond = (ushort)dateTimePicker1.Second; try { SetSTime.SetSystemTime(ref MySystemTime); SystemTime st2 = new SystemTime(); SetSTime.GetSystemTime(ref st2); } catch (Exception) { throw; } } } public struct SystemTime { public ushort vYear; public ushort vMonth; public ushort vDayOfWeek; public ushort vDay; public ushort vHour; public ushort vMinute; public ushort vSecond; public ushort vMiliseconds; }