How can I programmatically stop/start a windows service on a remote box?

后端 未结 9 1744
清歌不尽
清歌不尽 2020-11-27 05:17

I want to write a console or Click Once WinForms app that will programmatically stop and/or start a windows service on a remote box.

Both boxes are running .NET 3.5

9条回答
  •  余生分开走
    2020-11-27 05:37

    if you need to get the name of the Service:

    run this from the command line:

    sc query

    You will see for example, that SQL Server's service name is 'MSSQL$SQLEXPRESS'.

    So to stop the SQL Server service in C#:

            ServiceController controller = new ServiceController();
            controller.MachineName = "Machine1";
            controller.ServiceName = "MSSQL$SQLEXPRESS";
    
            if(controller.Status == ServiceControllerStatus.Running)
                controller.Stop();
    
            controller.WaitForStatus(ServiceControllerStatus.Stopped);
    

提交回复
热议问题