首先我做: 在Form1的顶部:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);
[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek; // ignored for the SetLocalTime function
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
private int day;
private int month;
private int year;
private int hour;
private int minute;
然后我有两种方法可以提前一天在系统中移动时间,另一种方法是将时间移回当天的当天:
private void ChangeTimeForword()
{
dt = DateTime.Now.AddDays(1);
day = dt.Day;
month = dt.Month;
year = dt.Year;
hour = dt.Hour;
minute = dt.Minute;
SYSTEMTIME time = new SYSTEMTIME();
time.wDay = (ushort)day;
time.wMonth = (ushort)month;
time.wYear = (ushort)year;
time.wHour = (ushort)hour;
time.wMinute = (ushort)minute;
if (!SetLocalTime(ref time))
{
// The native function call failed, so throw an exception
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
private void ChangeTimeOriginal()
{
try
{
dt = DateTime.Now.AddDays(-1);
day = dt.Day;
month = dt.Month;
year = dt.Year;
hour = dt.Hour;
minute = dt.Minute;
SYSTEMTIME time = new SYSTEMTIME();
time.wDay = (ushort)day;
time.wMonth = (ushort)month;
time.wYear = (ushort)year;
time.wHour = (ushort)hour;
time.wMinute = (ushort)minute;
if (!SetLocalTime(ref time))
{
// The native function call failed, so throw an exception
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
catch (Exception err)
{
MessageBox.Show("Error" + err.ToString());
}
}
所以它第一次起作用但是日期改变了。 首先,日期是9/30/2013 7:43 PM 然后它现在改为日期:2013年6月11日,它在6月12日和6月11日之间移动
奇怪。那是为什么?