在Windows 7上使用C#设置系统时间

时间:2011-11-04 13:04:03

标签: c# windows-7

如何在Windows 7上使用C#以编程方式设置本地时间?

2 个答案:

答案 0 :(得分:7)

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME 
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}

[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetSystemTime( [In] ref SYSTEMTIME st );

SYSTEMTIME st = new SYSTEMTIME();
st.wYear = 2003; // must be short 
st.wMonth = 5; 
st.wDay = 22;
st.wHour = 0;
st.wMinute = 0;
st.wSecond = 0;

SetSystemTime(ref st);

答案 1 :(得分:-1)

我不确定是否有.NET方法可以做到这一点,但Win32中有一个名为SetLocalTime的函数。 http://msdn.microsoft.com/en-us/library/ms724936(VS.85).aspx 请注意,用户可能需要提升权限才能执行此操作。