1

这是 Form1 中的代码:

private DateTime dt;
        [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 NotifyIcon notifyIcon1;

        public Form1()
        {
            InitializeComponent();

            notifyIcon1 = new NotifyIcon();
            SetBalloonTip();
            dt = GetNetworkTime();
            SYSTEMTIME time = new SYSTEMTIME();
            time.wDay = 1;
            time.wMonth = 5;
            time.wYear = 2011;
            time.wHour = 12;
            time.wMinute = 15;

            if (!SetLocalTime(ref time))
            {
                // The native function call failed, so throw an exception
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }

GetNetworkTime() 返回当前的实时时间。例如,我想要做的是花费 dt 时间并在另一天添加它。

time.wDay = 1;
time.wMonth = 5;
time.wYear = 2011;
time.wHour = 12;
time.wMinute = 15;

所以最后可变时间将返回我第二天的日期和时间,而不是 5/1/2011 12:15 PM

例如,如果现在的时间和日期是:2013 年 9 月 27 日下午 3:39 所以现在我希望它是 2013 年 9 月 28 日下午 4:39

每次为当前时间增加一天和一个小时!

然后我想等待 5 分钟,让时间自动重置回原来的时间。

稍后我将使用一个按钮单击事件,使其每次单击都添加到当前时间一天和一个小时,然后一个计时器等待 5 分钟并将其重置回原始时间。

我该怎么做 ?我需要以某种方式解析 dt 变量并添加一天和一小时并使用 SYSTEMTIME 将其设置为当前日期和时间,然后再次获取当前日期和时间解析它并在 5 分钟后设置它。

4

1 回答 1

4
dt = GetNetworkTime().AddDays(1);
于 2013-09-27T12:46:23.727 回答