Suriyan Suresh
Suriyan Suresh

Reputation: 3024

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

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

Dim pstart As New ProcessStartInfo
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.System)
Dim p As New Process
pstart.FileName = path + "\cmd.exe"
pstart.UseShellExecute = False
pstart.CreateNoWindow = True
pstart.WorkingDirectory = path
pstart.FileName = "cmd.exe"
pstart.Arguments = " net start mysql"
p.StartInfo = pstart
p.Start()

i have used process class but no result

Upvotes: 10

Views: 22138

Answers (3)

Alex Lyman
Alex Lyman

Reputation: 15975

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();
}

Upvotes: 6

heavyd
heavyd

Reputation: 17751

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

pstart.Arguments = "/c net start mysql"

Upvotes: 3

Kirtan
Kirtan

Reputation: 21685

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();

Upvotes: 27

Related Questions