简体   繁体   中英

Set system time using C# on windows 7

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

[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);

I'm not sure if there is a .NET way of doing this, but there is a function in Win32 called SetLocalTime. http://msdn.microsoft.com/en-us/library/ms724936(VS.85).aspx Note that the user might need elevated privileges to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM