C#设置系统时间可以调用Win32的API,函数有:SetSystemTime(), GetSystemTimer(), SetLocalTime(), GetLocalTime()。
上述函数,其输入参数必须是一个下面这样的结构体,其成员变量类型必须是ushort,成员变量不能改变顺序。
[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;
}
调用Win32的API,根据需要选用:
public class Win32
{
[DllImport("Kernel32.dll")]
public static extern bool SetSystemTime(ref SystemTime sysTime );
[DllImport("Kernel32.dll")]
public static extern bool SetLocalTime(ref SystemTime sysTime);
[DllImport("Kernel32.dll")]
public static extern void GetSystemTime(ref SystemTime sysTime);
[DllImport("Kernel32.dll")]
public static extern void GetLocalTi