gpt4 book ai didi

c# - 如何远程重启服务?

转载 作者:可可西里 更新时间:2023-11-01 08:07:56 50 4
gpt4 key购买 nike

我可以从 .net 项目远程启动或停止服务。

ConnectionOptions options = new ConnectionOptions();
options.Username = @"192.168.36.22\test";
options.Password = "test";
ManagementScope scope = new ManagementScope(@"\\192.168.36.22\root\cimv2", options);
scope.Connect();


ManagementOperationObserver Stop = new ManagementOperationObserver();
Stop.Completed += new CompletedEventHandler(Stop_CallBack);
try
{
string NameServices = "ArcGIS Server";
WqlObjectQuery query = new WqlObjectQuery("SELECT * FROM Win32_Service WHERE Name=\"" + NameServices + "\"");
ManagementObjectSearcher find = new ManagementObjectSearcher(scope, query);
foreach (ManagementObject spooler in find.Get())
{
spooler.InvokeMethod("StopService", new object[] { });
spooler.InvokeMethod(Start, "StopService", new object[] { });
}
}
....

如何重新启动此服务?

最佳答案

您可以像这样使用 ServiceController 类:

ServiceController sc = new ServiceController("ArcGIS Server", "192.168.36.22");

sc.Start();
sc.Stop();

这样您就不必编写所有代码来与 WMI 交互。请注意,要使用 ServiceController 类,您必须添加对 System.ServiceProcess 程序集的引用。

关于c# - 如何远程重启服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405884/

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