首页 > 开发 > 综合 > 正文

使用C#设置系统时间

2024-07-21 02:19:00
字体:
来源:转载
供稿:网友
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;

namespace windowsapplication1
{
[structlayout(layoutkind.sequential)]
public 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 wmiliseconds;
}

public class win32
{
[dllimport("kernel32.dll")]
public static extern bool setsystemtime( ref systemtime systime );
[dllimport("kernel32.dll")]
public static extern void getsystemtime(ref systemtime systime);
}


/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.button button1;
private system.windows.forms.textbox textbox1;
private system.windows.forms.textbox textbox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;

public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();

//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}

#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.button1 = new system.windows.forms.button();
this.textbox1 = new system.windows.forms.textbox();
this.textbox2 = new system.windows.forms.textbox();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(188, 174);
this.button1.name = "button1";
this.button1.tabindex = 0;
this.button1.text = "button1";
this.button1.click += new system.eventhandler(this.button1_click);
//
// textbox1
//
this.textbox1.location = new system.drawing.point(56, 12);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(510, 21);
this.textbox1.tabindex = 1;
this.textbox1.text = "textbox1";
//
// textbox2
//
this.textbox2.location = new system.drawing.point(58, 50);
this.textbox2.name = "textbox2";
this.textbox2.size = new system.drawing.size(506, 21);
this.textbox2.tabindex = 2;
this.textbox2.text = "textbox2";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(602, 273);
this.controls.add(this.textbox2);
this.controls.add(this.textbox1);
this.controls.add(this.button1);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}

private void button1_click(object sender, system.eventargs e)
{
textbox1.text = datetime.now.tolongdatestring() + datetime.now.tolongtimestring();
systemtime systime = new systemtime();

systime.wyear = convert.touint16(datetime.now.year);
systime.wmonth = convert.touint16(datetime.now.month);
//处置北京时间
int nbeijinghour = datetime.now.hour - 8;
if (nbeijinghour <= 0)
{
nbeijinghour += 24;
systime.wday= convert.touint16(datetime.now.day - 1);
systime.wdayofweek = convert.touint16(datetime.now.dayofweek - 1);
}
else
{
systime.wday= convert.touint16(datetime.now.day);
systime.wdayofweek = convert.touint16(datetime.now.dayofweek);
}
systime.whour = convert.touint16(nbeijinghour);
systime.wminute = convert.touint16(datetime.now.minute);
systime.wsecond = convert.touint16(datetime.now.second);
systime.wmiliseconds = convert.touint16(datetime.now.millisecond);

win32.setsystemtime(ref systime);
textbox2.text = datetime.now.tolongdatestring() + datetime.now.tolongtimestring();
}


}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表