gpt4 book ai didi

c# - 设置机器时间C#

转载 作者:太空狗 更新时间:2023-10-29 22:55:37 24 4
gpt4 key购买 nike

在 C# 中设置机器时间的最佳方法是什么?

最佳答案

您可能需要使用 Win32 API 来执行此操作,因为我相当确定框架中没有内置任何内容:

[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;
}
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetSystemTime(ref SYSTEMTIME theDateTime );

PInvoke.net 有一个更完整的例子, 代码有点密集,但是一个简单易懂的摘录是这样的:

SYSTEMTIME st = new SYSTEMTIME();
GetSystemTime(ref st);
// Adds one hour to the time that was retrieved from GetSystemTime
st.wHour = (ushort)(st.wHour + 1 % 24);
var result = SetSystemTime(ref st);
if (result == false)
{
// Something went wrong
}
else
{
// The time will now be 1hr later than it was previously
}

相关的特定 Win32 API 是 SetSystemTime , GetSystemTimeSYSTEMTIME结构。

关于c# - 设置机器时间C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3391691/

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