0

首先我在做:在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());
            }
        }

所以它第一次起作用,但后来日期改变了。首先日期是 2013 年 9 月 30 日晚上 7:43,然后现在更改为日期:2013 年 6 月 11 日,它在 6/12 和 6/11 之间移动

奇怪的。这是为什么 ?

4

0 回答 0