C# 设置系统时间
2007-05-24 14:52
465 查看
在程序中有时需要系统时间准确,为了方便修改时间,做了个时间修改的,代码如下
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 Qualification
...{
public partial class FormDateSet : Form
...{
public FormDateSet()
...{
InitializeComponent();
}
//调用Kernel32.DLL
[DllImport("Kernel32.dll")]
public static extern void GetLocalTime(SystemTime st);
[DllImport("Kernel32.dll")]
public static extern void SetLocalTime(SystemTime st);
[StructLayout(LayoutKind.Sequential)]
public class SystemTime
...{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort Whour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
private void FormDateSet_Load(object sender, EventArgs e)
...{
}
//取得当前系统时间
private void timer1_Tick(object sender, EventArgs e)
...{
SystemTime st = new SystemTime();
GetLocalTime(st);
this.textBox1.Text = st.wYear.ToString()+"-";
this.textBox1.Text = this.textBox1.Text + st.wMonth.ToString() + "-";
this.textBox1.Text = this.textBox1.Text + st.wDay.ToString() + " ";
this.textBox1.Text = this.textBox1.Text + st.Whour.ToString() + ":" + st.wMinute.ToString() + ":" + st.wSecond.ToString();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
...{
}
//校对系统时间
private void Btn_set_Click(object sender, EventArgs e)
...{
try
...{
SystemTime st = new SystemTime();
st.wYear = (ushort)this.dateTimePicker1.Value.Year;
st.wMonth = (ushort)this.dateTimePicker1.Value.Month;
st.wDay = (ushort)this.dateTimePicker1.Value.Day;
st.Whour = (ushort)this.dateTimePicker1.Value.Hour;
st.wMinute = (ushort)this.dateTimePicker1.Value.Minute;
st.wSecond = (ushort)this.dateTimePicker1.Value.Second;
SetLocalTime(st);
MessageBox.Show("系统时间设置成功!", "系统时间设置成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
...{
MessageBox.Show("系统时间设置失败!", "设置失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
...{}
}
}
}
相关文章推荐
- c# 设置系统时间 张宇轩
- 嵌入开发(WinCE) 用C#设置系统时间
- C#设置系统时间的方法实例详解
- 用C#设置系统时间和本地时间
- C#获取北京时间与设置系统时间
- C#设置系统日期和时间的代码
- c# API 设置系统时间
- 使用C#代码设置系统日期和时间
- C#设置系统时间
- C#设置系统时间
- C#设置系统日期和时间
- c#在WIN7下设置系统时间
- 用C#设置系统时间和本地时间
- C# 设置CE系统的时间
- debian设置系统时间
- ubuntu双系统 开机顺序 开机选择时间 设置
- linux中显示/设置系统时间
- 设置系统时间
- 设置CentOS Linux系统时间自动同步
- linux 的date命令及系统时间设置