如何远程重启服务? [英] How to restart service remotely?

查看:345
本文介绍了如何远程重启服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can start or stop service remotely from .net project.

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类:

You could use the ServiceController class like so:

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

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

这省去了编写所有与WMI交互的代码的麻烦.请注意,要使用ServiceController类,您必须添加对System.ServiceProcess程序集的引用.

This saves you having to write all that code to interact with WMI. Note to use the ServiceController class, you'll have to add a reference to the System.ServiceProcess assembly.

这篇关于如何远程重启服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆