Arjun
Arjun

Reputation: 1069

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.

    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.

Upvotes: 4

Views: 11414

Answers (1)

Edwin de Koning
Edwin de Koning

Reputation: 14387

You will need to instantiate the ServiceController with the overloaded constructor that accepts a machine name, like this:

ServiceController service = new ServiceController(serviceName, machineName);

Upvotes: 6

Related Questions