我编写了以下类来更改系统日期时间,但我不明白为什么它在一天中的不同时间运行不同:
SetTime()
被设置为系统时间。SetTime()
+1Hr的时间被设置为系统时间,额外的一小时从哪里来?.
public class SystemDateTimeController
{
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
private extern static bool Win32SetSystemTime(ref SYSTEMTIME sysTime);
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;
}
public static void SetTime(DateTime NewDateTime)
{
try
{
SYSTEMTIME systime = new SYSTEMTIME();
systime.wMilliseconds = (ushort)NewDateTime.Millisecond;
systime.wSecond = (ushort)NewDateTime.Second;
systime.wMinute = (ushort)NewDateTime.Minute;
systime.wHour = (ushort)NewDateTime.Hour;
systime.wDayOfWeek = (ushort)NewDateTime.DayOfWeek;
systime.wDay = (ushort)NewDateTime.Day;
systime.wMonth = (ushort)NewDateTime.Month;
systime.wYear = (ushort)NewDateTime.Year;
Win32SetSystemTime(ref systime);
}
catch (Exception e)
{
Log("Failed to set system date time to: " + NewDateTime.ToString() + ". Exception: " + e.ToString());
}
}
}
我知道传递给SetTime()
和测试目的的确切时间,并消除与源有关的任何可能性,我什至传递了一个硬编码字符串:Convert.ToDateTime("07/06/2018 13:00:00");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句