如何每隔一分钟修改一次系统时间?

somtv 2009-05-20 03:12:34
我想让程序每隔一段时间自动去修改一次系统时间,但出现了下面的情况:

例如:当前系统时间为 2008.08.10 23:58 时, 我想让系统时间增加30分钟
然后让系统时间变为:2008.08.11 00:28

但我的代码不知道应该如何去实现.
代码如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ChangeDataTime
{
public partial class Form1 : Form
{
int i = 0;
DateTime nowtime = new DateTime();
public Form1()
{
InitializeComponent();


}

[DllImport("kernel32.dll")]
private static extern bool SetLocalTime(ref SYSTEMTIME time);

[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short year;
public short month;
public short day;
public short hour;
public short minute;
public short second;
}



public static void SetDate(DateTime dt)
{
SYSTEMTIME st;
st.year = (short)dt.Year;
st.month = (short)dt.Month;
st.day = (short)dt.Day;
st.hour = (short)(dt.Hour + 1);
st.minute = (short)(dt.Minute + 13);
st.second = (short)(dt.Second);

SetLocalTime(ref st);
}



private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
nowtime = System.DateTime.Now;
SetDate(nowtime);
}

private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}



}
}



请各位帮帮忙.
...全文
131 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbb332 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 skyaspnet 的回复:]
在Global.asax文件里使用Timer类,每隔固定时间进行更改
[/Quote]
up
arlang 2009-05-21
  • 打赏
  • 举报
回复
为何会有如此奇特的需求?系统时钟是保证 定时器正常 运行的非常 重要的东东,如果随意改动会影响timer中
函数的执行
somtv 2009-05-21
  • 打赏
  • 举报
回复
公司要做奇怪的事...也就有了如此需求....

我解决掉了...
skyaspnet 2009-05-20
  • 打赏
  • 举报
回复
在Global.asax文件里使用Timer类,每隔固定时间进行更改

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧