我從網絡論壇發現了一個示例代碼。當我開始學習它。我發現輸出很奇怪。以C#獲取並設置系統時間
爲什麼代碼不能正常工作?
當我運行下面的代碼時,它總是向前走8小時。
使用VS2005和WinXP。謝謝。
class Tester
{
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
true)]
private static extern bool GetSystemTime(out SYSTEMTIME systemTime);
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
true)]
private static extern bool SetSystemTime(ref SYSTEMTIME systemTime);
struct SYSTEMTIME
{
internal short wYear;
internal short wMonth;
internal short wDayOfWeek;
internal short wDay;
internal short wHour;
internal short wMinute;
internal short wSecond;
internal short wMilliseconds;
}
static void Main()
{
SYSTEMTIME st;
if (GetSystemTime(out st))
{
st.wHour = 2; // new system time was set to nearby 10:00 AM, 2 + 8
// If i replace the line with below one.
// st.wHour = 18; // new system time was set to nearby 2:00 AM next day, 18 + 8 = 26, 26 - 24. go to next day!
if (SetSystemTime(ref st))
Console.WriteLine("success");
else
Console.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
else
Console.WriteLine("GetSystemTime failed: {0}",
System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
}
@Both。非常感謝。 – 2010-06-29 08:43:47