本文介绍了以编程方式更改系统日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何更改本地系统的日期&时间以C#编程?
How can I change the local system's date & time programmatically with C#?
推荐答案
我已经转发了这里,以提高清晰度
I have reposted it here to improve clarity.
定义此结构:
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
添加以下 extern
方法到你的类:
Add the following extern
method to your class:
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetSystemTime(ref SYSTEMTIME st);
然后用这样的结构调用该方法:
Then call the method with an instance of your struct like this:
SYSTEMTIME st = new SYSTEMTIME();
st.wYear = 2009; // must be short
st.wMonth = 1;
st.wDay = 1;
st.wHour = 0;
st.wMinute = 0;
st.wSecond = 0;
SetSystemTime(ref st); // invoke this method.
这篇关于以编程方式更改系统日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!