欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现修改系统时间的方法

程序员文章站 2024-02-11 23:57:58
本文所述c#获取和修改系统时间的实现步骤为:系统的时间从 systemtime 结构体中取出,并显示在textbox1上,从setdate,settime控件中获取年,月,...

本文所述c#获取和修改系统时间的实现步骤为:系统的时间从 systemtime 结构体中取出,并显示在textbox1上,从setdate,settime控件中获取年,月,日,小时,分钟,秒信息,存入systemtime结构体中,然后使用setlocaltime(ref systemtime)设置为用户指定的时间。本代码编译后会有一个易于操作的窗体。

完整功能代码如下:

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
namespace changesystime
{
 /// <summary>
 /// form1 的摘要说明。
 /// </summary>
 public class form1 : system.windows.forms.form
 {
 private system.windows.forms.groupbox groupbox1;
 private system.windows.forms.textbox textbox1;
 private system.windows.forms.groupbox groupbox2;
 private system.windows.forms.button button1;
 private system.windows.forms.button button2;
 private system.timers.timer timer1;
 private system.windows.forms.datetimepicker setdate;
 private system.windows.forms.datetimepicker settime;
 private system.componentmodel.icontainer components;
 [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;
 }
 
 // 用于设置系统时间
 [dllimport("kernel32.dll")]
 public static extern bool setlocaltime( ref systemtime systime );
 // 用于获得系统时间
 [dllimport("kernel32.dll")]
 public static extern void getlocaltime(ref systemtime systime);
 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.groupbox1 = new system.windows.forms.groupbox();
  this.textbox1 = new system.windows.forms.textbox();
  this.groupbox2 = new system.windows.forms.groupbox();
  this.settime = new system.windows.forms.datetimepicker();
  this.setdate = new system.windows.forms.datetimepicker();
  this.button1 = new system.windows.forms.button();
  this.button2 = new system.windows.forms.button();
  this.timer1 = new system.timers.timer();
  this.groupbox1.suspendlayout();
  this.groupbox2.suspendlayout();
  ((system.componentmodel.isupportinitialize)(this.timer1)).begininit();
  this.suspendlayout();
  // 
  // groupbox1
  // 
  this.groupbox1.controls.add(this.textbox1);
  this.groupbox1.location = new system.drawing.point(32, 24);
  this.groupbox1.name = "groupbox1";
  this.groupbox1.size = new system.drawing.size(216, 64);
  this.groupbox1.tabindex = 0;
  this.groupbox1.tabstop = false;
  this.groupbox1.text = "系统当前时间";
  // 
  // textbox1
  // 
  this.textbox1.location = new system.drawing.point(16, 24);
  this.textbox1.name = "textbox1";
  this.textbox1.readonly = true;
  this.textbox1.size = new system.drawing.size(184, 21);
  this.textbox1.tabindex = 1;
  this.textbox1.text = "";
  // 
  // groupbox2
  // 
  this.groupbox2.controls.add(this.settime);
  this.groupbox2.controls.add(this.setdate);
  this.groupbox2.location = new system.drawing.point(32, 112);
  this.groupbox2.name = "groupbox2";
  this.groupbox2.size = new system.drawing.size(216, 64);
  this.groupbox2.tabindex = 1;
  this.groupbox2.tabstop = false;
  this.groupbox2.text = "时间设置为";
  // 
  // settime
  // 
  this.settime.format = system.windows.forms.datetimepickerformat.time;
  this.settime.location = new system.drawing.point(128, 24);
  this.settime.name = "settime";
  this.settime.showupdown = true;
  this.settime.size = new system.drawing.size(72, 21);
  this.settime.tabindex = 1;
  this.settime.tabstop = false;
  // 
  // setdate
  // 
  this.setdate.format = system.windows.forms.datetimepickerformat.short;
  this.setdate.location = new system.drawing.point(8, 24);
  this.setdate.name = "setdate";
  this.setdate.size = new system.drawing.size(104, 21);
  this.setdate.tabindex = 0;
  // 
  // button1
  // 
  this.button1.location = new system.drawing.point(40, 200);
  this.button1.name = "button1";
  this.button1.size = new system.drawing.size(64, 32);
  this.button1.tabindex = 2;
  this.button1.text = "设置";
  this.button1.click += new system.eventhandler(this.button1_click);
  // 
  // button2
  // 
  this.button2.location = new system.drawing.point(168, 200);
  this.button2.name = "button2";
  this.button2.size = new system.drawing.size(64, 32);
  this.button2.tabindex = 3;
  this.button2.text = "退出";
  this.button2.click += new system.eventhandler(this.button2_click);
  // 
  // timer1
  // 
  this.timer1.enabled = true;
  this.timer1.synchronizingobject = this;
  this.timer1.elapsed += new system.timers.elapsedeventhandler(this.timer1_elapsed);
  // 
  // form1
  // 
  this.autoscalebasesize = new system.drawing.size(6, 14);
  this.clientsize = new system.drawing.size(280, 261);
  this.controls.add(this.button2);
  this.controls.add(this.button1);
  this.controls.add(this.groupbox2);
  this.controls.add(this.groupbox1);
  this.name = "form1";
  this.text = "获取和设置系统时间";
  this.groupbox1.resumelayout(false);
  this.groupbox2.resumelayout(false);
  ((system.componentmodel.isupportinitialize)(this.timer1)).endinit();
  this.resumelayout(false);

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

 private void button2_click(object sender, system.eventargs e)
 {
  this.close(); // 关闭当前窗体
 }

 private void timer1_elapsed(object sender, system.timers.elapsedeventargs e)
 {
  // 清除textbox1上的字符串
  textbox1.clear();
  // 创建systemtime结构体,用于接收系统当前时间
  systemtime systemtime = new systemtime();
  getlocaltime(ref systemtime); // 获得系统的时间并存在systemtime结构体中
  // 将系统的时间从 systemtime 结构体中中取出,并显示在textbox1上
  textbox1.text += systemtime.wyear.tostring() +"-";
  textbox1.text += systemtime.wmonth.tostring() + "-";
  textbox1.text += systemtime.wday.tostring() + " ";
  textbox1.text += systemtime.whour.tostring() + ":";
  textbox1.text += systemtime.wminute.tostring() + ":";
  textbox1.text += systemtime.wsecond.tostring();
 // textbox1.refresh(); 
 }
 private void button1_click(object sender, system.eventargs e)
 {
  // 创建systemtime结构体,用于接收用户设置的时间
  systemtime systemtime = new systemtime();
  // 从setdate,settime控件中获取年,月,日,小时,分钟,秒信息,存入systemtime结构体中
      systemtime.wyear = (ushort)setdate.value.year;
  systemtime.wmonth = (ushort)setdate.value.month;
  systemtime.wday = (ushort)setdate.value.day;
  systemtime.whour = (ushort)settime.value.hour;
  systemtime.wminute = (ushort)settime.value.minute;
  systemtime.wsecond = (ushort)settime.value.second;
  // 将系统的时间设置为用户指定的时间
  setlocaltime(ref systemtime);
 }
 }
}