如何使用 C# 将 Windows 系统时钟设置为正确的本地时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6083200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:53:49  来源:igfitidea点击:

How do I set the Windows system clock to the correct local time using C#?

c#.netwindowswindows-xpclock

提问by The Mask

How do I set the Windows system clock to the correct local time using C#?

如何使用 C# 将 Windows 系统时钟设置为正确的本地时间?

回答by Cody Gray

You will need to P/Invoke the SetLocalTimefunctionfrom the Windows API. Declare it like this in C#:

您将需要从 Windows API P/InvokeSetLocalTime函数。在 C# 中这样声明:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);

[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEMTIME
{
    public ushort wYear;
    public ushort wMonth;
    public ushort wDayOfWeek;    // ignored for the SetLocalTime function
    public ushort wDay;
    public ushort wHour;
    public ushort wMinute;
    public ushort wSecond;
    public ushort wMilliseconds;
}

To set the time, you simply initialize an instance of the SYSTEMTIMEstructure with the appropriate values, and call the function. Sample code:

要设置时间,您只需SYSTEMTIME使用适当的值初始化结构的一个实例,然后调用该函数。示例代码:

SYSTEMTIME time = new SYSTEMTIME();
time.wDay = 1;
time.wMonth = 5;
time.wYear = 2011;
time.wHour = 12;
time.wMinute = 15;

if (!SetLocalTime(ref time))
{
    // The native function call failed, so throw an exception
    throw new Win32Exception(Marshal.GetLastWin32Error());
}

However, note that the calling process must have the appropriate privileges in order to call this function. In Windows Vista and later, this means you will have to request process elevation.

但是,请注意,调用进程必须具有适当的权限才能调用此函数。在 Windows Vista 和更高版本中,这意味着您必须请求进程提升。



Alternatively, you could use the SetSystemTimefunction, which allows you to set the time in UTC (Coordinated Universal Time). The same SYSTEMTIMEstructure is used, and the two functions are called in an identical fashion.

或者,您可以使用SetSystemTime函数,它允许您以 UTC(协调世界时)设置时间。使用相同的SYSTEMTIME结构,并且以相同的方式调用这两个函数。

回答by Teoman Soygul

.NET does not expose a function for that but you can use Win32 API SetSystemTime(in kernel32.dll) method. To get UTC time you should use an NTP Protocol Clientand then adjust that time to the local time according to your regional settings.

.NET 没有为此公开函数,但您可以使用 Win32 API SetSystemTime(在 kernel32.dll 中)方法。要获得 UTC 时间,您应该使用NTP 协议客户端,然后根据您的区域设置将该时间调整为本地时间。

public struct SYSTEMTIME
{    
  public ushort wYear,wMonth,wDayOfWeek,wDay,wHour,wMinute,wSecond,wMilliseconds;
}

[DllImport("kernel32.dll")]
public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);

SYSTEMTIME systime = new SYSTEMTIME();
systime = ... // Set the UTC time here
SetSystemTime(ref systime);

回答by Joel Mueller

Here's a couple of articles on how to do that, complete with querying an atomic clock for the correct time.

这里有几篇关于如何做到这一点的文章,包括查询原子钟的正确时间。

http://www.codeproject.com/KB/IP/ntpclient.aspx

http://www.codeproject.com/KB/IP/ntpclient.aspx

http://www.codeproject.com/KB/datetime/SNTPClient.aspx

http://www.codeproject.com/KB/datetime/SNTPClient.aspx

回答by Edgar

To get around the SE_SYSTEMTIME_NAME privilege issue, try creating a scheduled task to run your application and enable "Run with highest privileges."

要解决 SE_SYSTEMTIME_NAME 权限问题,请尝试创建计划任务来运行您的应用程序并启用“以最高权限运行”。