gpt4 book ai didi

c# - 以编程方式更改系统日期

转载 作者:IT王子 更新时间:2023-10-29 03:48:48 25 4
gpt4 key购买 nike

如何使用 C# 以编程方式更改本地系统的日期和时间?

最佳答案

Here is where I found the answer. ;我已在此处重新发布它以提高清晰度。

定义这个结构:

[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 方法添加到您的类中:

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetSystemTime(ref SYSTEMTIME st);

然后像这样使用结构的实例调用该方法:

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.

关于c# - 以编程方式更改系统日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com