我试图用更新我的系统时间,下面来更新系统的日期和/或时间:如何使用.NET
[StructLayout(LayoutKind.Sequential)]
private 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 wMilliseconds;
}
[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
private extern static void Win32GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
private extern static bool Win32SetSystemTime(ref SYSTEMTIME lpSystemTime);
public void SetTime()
{
TimeSystem correctTime = new TimeSystem();
DateTime sysTime = correctTime.GetSystemTime();
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME systime = new SYSTEMTIME();
Win32GetSystemTime(ref systime);
// Set the system clock ahead one hour.
systime.wYear = (ushort)sysTime.Year;
systime.wMonth = (ushort)sysTime.Month;
systime.wDayOfWeek = (ushort)sysTime.DayOfWeek;
systime.wDay = (ushort)sysTime.Day;
systime.wHour = (ushort)sysTime.Hour;
systime.wMinute = (ushort)sysTime.Minute;
systime.wSecond = (ushort)sysTime.Second;
systime.wMilliseconds = (ushort)sysTime.Millisecond;
Win32SetSystemTime(ref systime);
}
当调试一切看起来不错,所有的值都是正确的,但是当它调用Win32SetSystemTime(ref systime)系统的实际时间(显示时间)不会更改并保持不变。奇怪的是,当我调用Win32GetSystemTime(ref systime)时,它给了我新的更新时间。有人能给我一些帮助吗?
我看到,TimeSystem是从基本系统类非,你确定TimeSystem不改变时间?尝试评论Win32SetSystemTime(ref systime);行 – Avram 2009-02-08 00:30:41