how do start/stop services using net stop command in c#

后端 未结 3 366
余生分开走
余生分开走 2020-12-06 02:47

how do start/stop services using net stop command in c# for example

Dim pstart As New ProcessStartInfo
Dim path As String = Environment.GetFolderPath(Environ         


        
相关标签:
3条回答
  • 2020-12-06 03:14

    You might want to take a look at the System.ServiceProcess.ServiceController class, which provides a managed interface to Windows' Services.

    In this case:

    var mysql = new System.ServiceProcess.ServiceController("mysql");
    if (mysql .Status == ServiceControllerStatus.Stopped) {
       mysql.Start();
    }
    
    0 讨论(0)
  • 2020-12-06 03:23

    Instead of using a crude method like Process.Start, you can use the ServiceController class to start/stop a particular service on a local/remote machine.

    using System.ServiceProcess;
    ServiceController controller  = new ServiceController();
    
    controller.MachineName = ".";
    controller.ServiceName = "mysql";
    
    // Start the service
    controller.Start();
    
    // Stop the service
    controller.Stop();
    
    0 讨论(0)
  • 2020-12-06 03:23

    You need to pass the "/c" switch to cmd.exe

    pstart.Arguments = "/c net start mysql"
    
    0 讨论(0)
提交回复
热议问题