windows 在 C# 中远程设置时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32718/
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 11:07:40  来源:igfitidea点击:

Setting time zone remotely in C#

c#.netwindowslocalizationtimezone

提问by Jim Blake

How do you set the Windows time zone on the local machine programmatically in C#? Using an interactive tool is not an option because the remote units have no user interface or users. The remote machine is running .NET 2.0 and Windows XP Embedded and a local app that communicates with a central server (via web service) for automated direction of its tasks. We can deliver a command to synch to a certain time/zone combination, but what code can be put in the local app to accomplish the change? The equipment is not imaged for specific locations before installation, so in order to use any equipment at any location, we have to be able to synch this information.

如何在 C# 中以编程方式在本地机器上设置 Windows 时区?使用交互式工具不是一种选择,因为远程单元没有用户界面或用户。远程机器运行 .NET 2.0 和 Windows XP Embedded 以及一个本地应用程序,该应用程序与中央服务器(通过 Web 服务)通信以自动指导其任务。我们可以下发一个命令来同步到某个时区组合,但是在本地应用程序中可以放置什么代码来完成更改?设备在安装前并未针对特定位置进行成像,因此为了在任何位置使用任何设备,我们必须能够同步这些信息。

回答by

SetTimeZoneInformationshould do what you need. You'll need to use P/Invoketo get at it.

SetTimeZoneInformation应该做你需要的。您需要使用P/Invoke来获取它。

Note also that you'll need to possess and enable the SE_TIME_ZONE_NAME privilege.

另请注意,您需要拥有并启用 SE_TIME_ZONE_NAME 权限。

回答by offler

santosh you are definitly correct. RunDLL32 shell32.dll,Control_RunDLL %SystemRoot%\system32\TIMEDATE.cpl,,/Z %1 is deprecated for years and will neither run on Windows 2008, R2, Vista, 7, ... oh, yes i noticed that this forum seems to have a complete bug with recognizing the enter key. maybe someday the programmer will fix that problem.

santosh 你绝对是正确的。RunDLL32 shell32.dll,Control_RunDLL %SystemRoot%\system32\TIMEDATE.cpl,,/Z %1 已弃用多年,并且不会在 Windows 2008, R2, Vista, 7, ... 哦,是的,我注意到这个论坛识别回车键似乎有一个完整的错误。也许有一天程序员会解决这个问题。

回答by Santhosh

This is not working in Windows 7. I have tried with the following environment windows 7 VSTS-2008 It is opening Change Time Zone Window as we do it manually

这在 Windows 7 中不起作用。我尝试使用以下环境 windows 7 VSTS-2008 它正在打开更改时区窗口,因为我们手动执行此操作

回答by Oliver

Instead of just setting the time zone of some systems you should consider to use the Windows Time Service. It will not only handle the time zone. It will also take care about correct settings of date and time itself.

您应该考虑使用 Windows 时间服务,而不是仅仅设置某些系统的时区。它不仅会处理时区。它还将注意正确设置日期和时间本身。

Take a look at: How to synchronize the time with the Windows Time service in Windows XP

看一看:如何在Windows XP中与Windows Time服务同步时间

Even if you maybe going to make all these settings within Vista or Windows 7 take a look at this here.

即使您可能要在 Vista 或 Windows 7 中进行所有这些设置,也请查看此处

回答by Jason Bunting

Try this...

尝试这个...

First, you need to find, in the registry, the keythat represents the zone you want ("Central Standard Time" is an example). Those are located here:

首先,您需要在注册表中找到代表您想要的区域的(“中央标准时间”就是一个例子)。这些位于:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\Current Version\Time Zones

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\Current Version\Time Zones

So, with that in mind, create a batch file, "SetTimeZone.bat" with the following line in it:

因此,考虑到这一点,创建一个批处理文件“SetTimeZone.bat”,其中包含以下行:

RunDLL32 shell32.dll,Control_RunDLL %SystemRoot%\system32\TIMEDATE.cpl,,/Z %1

RunDLL32 shell32.dll,Control_RunDLL %SystemRoot%\system32\TIMEDATE.cpl,,/Z %1

From C#, call:

在 C# 中,调用:

System.Diagnostics.Process.Start("SetTimeZone.bat", "The key of the time zone you want to set");

System.Diagnostics.Process.Start("SetTimeZone.bat", "您要设置的时区的键");

回答by Brian Leahy

You will run the risk of having incorrect data if you do not use UTC to transmit dates... If you change time zones on the device... you're dates will be even further off.

如果您不使用 UTC 来传输日期,您将面临数据不正确的风险……如果您更改设备上的时区……您的日期将更远。

You may want to use UTC and then calculate time in each timezone.

您可能希望使用 UTC,然后计算每个时区的时间。