简体   繁体   English

如何使用.net在远程计算机上启动/停止/重新启动Windows服务

[英]How do I start/stop/restart a windows service on a remote machine using .net

Currently I am developing desktop application that start/stop/restart windows services. 目前我正在开发启动/停止/重启Windows服务的桌面应用程序。

    public static void StartService(string serviceName, int timeoutMilliseconds)
    {
        ServiceController service = new ServiceController(serviceName);
        try
        {
            TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }
        catch
        {
            // ...
        }
    }

Now I want code to perform same action on remote system(other system on the same network). 现在我希望代码在远程系统(同一网络上的其他系统)上执行相同的操作。

Thanks. 谢谢。

您需要使用重载的接受机器名的构造函数来实例化ServiceController,如下所示:

ServiceController service = new ServiceController(serviceName, machineName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM