windows 使用 C# 在远程计算机上设置日期、时间和时区

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

Set date, time & time zone on remote computer using C#

c#.netwindowsdatetimetimezone

提问by Elangovan

How do you set date, time and the time zone setting of the remote computer using C#? Basically, I want to update the time on selected machines in the network.

你如何使用C#设置远程计算机的日期、时间和时区设置?基本上,我想更新网络中选定机器的时间。

Creating time server is not an option as I want to update the time only on the user selected computers. All the remote computers are running windows XP or higher operating system.

创建时间服务器不是一个选项,因为我只想更新用户选择的计算机上的时间。所有远程计算机都运行 Windows XP 或更高版本的操作系统。

采纳答案by Abbas

Executing a remote process through WMI and .NET are extensively documented online. You can execute the date, timeand tzutilDOS utilities to accomplish what you need. However, I think PsExecis the way to go.

通过 WMI 和 .NET 执行远程进程在网上有大量文档。您可以执行date,timetzutilDOS 实用程序来完成您需要的操作。但是,我认为PsExec是要走的路。

You only need to download the 1.6 MB utility once on your computer and use it to execute all kinds of remote processes on your XP machines. Here's how you can change the timezone to Central Time using PsExec:

您只需要在您的计算机上下载一次 1.6 MB 实用程序,并使用它在您的 XP 机器上执行各种远程进程。以下是使用 PsExec 将时区更改为中部时间的方法:

psexec \RemPC01 TZUTIL /s "Central Standard Time"

You can wrap this in a bit of .NET code and it should do what you want:

您可以将其包装在一些 .NET 代码中,它应该可以执行您想要的操作:

string remoteMachine = "RemPC01";
string appName = "psexec.exe";
string args = string.Format("\\{0} TZUTIL /s \"Central Standard Time\"", remoteMachine);
Process.Start(appName, args);

回答by TylarJ

I would recommend using PsExecto do this. Maybe using it in combination with your application. It gives you console access to remote computers, and the console command you are looking for is "time" to change the time.

我建议使用PsExec来做到这一点。也许将它与您的应用程序结合使用。它使您可以通过控制台访问远程计算机,您正在寻找的控制台命令是“时间”来更改时间。