C# 设置系统时间
来源:互联网 发布:淘宝网大码女装秋装 编辑:程序博客网 时间:2024/04/27 16:59
在程序中有时需要系统时间准确,为了方便修改时间,做了个时间修改的,代码如下
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
...{}
}
}
}
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# 设置系统时间
- C#设置系统时间
- C#设置系统时间
- c# 设置系统时间 张宇轩
- c# API 设置系统时间
- c#在WIN7下设置系统时间
- C# 设置CE系统的时间
- 用C#设置系统时间和本地时间
- 嵌入开发(WinCE) 用C#设置系统时间
- 使用C#代码设置系统日期和时间
- C#设置系统时间的方法实例详解
- C# 时间格式设置
- C# 时间格式设置
- 设置系统时间.
- 获取(设置)系统时间
- 设置系统时间
- PreparedStatement设置系统时间
- date 设置系统时间
- BI概述及发展趋势
- IT人 - 程序员每天该做的事(转帖)
- 论坛问题收集(3)----Delphi 组件撰写常见问题
- Myeclipse 5.5 GA 算号器源代码(分析)
- 去掉数组中重复的元素方法
- C# 设置系统时间
- StackTrace2String
- JAVA中native方法的使用
- 新的冷笑话~
- [J2SE]JDK5新特性——java.util.concurrent 线程池
- WAP手机上的问卷调查系统的构建
- 如何处理大数据量的查询
- 有关winpcap开发了几个有用的帖子!
- sql server事务全攻略