How can I start or stop system service like MSSQLSERVER?

How can I start or stop system service like MSSQLSERVER?

Normally I start system service in command line 'net start MSSQLSERVER'

How can I start or stop a service with C#? Certainly before you start a service we should detect if the service have been started, how to detect in C#?

[304 byte] By [CUIWEI] at [2007-12-16]
# 1
Hi Cui Wei,

you can use:

System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("MSSQLSERVER");
sc.Start();
sc.Stop();

use:

System.ServiceProcess.ServiceController scc[] = System.ServiceProcess.ServiceController.GetServices();

to get an array of all local services.

HTH,
SvenC

SvenC at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Thank you for this posting. I am looking to start/stop a service from an app. I added the code that you posted and am getting an error saying "The type or namespace name 'ServiceProcess' does not exist in the class or namespace 'System' (are you missing an assembly reference?)". I have not been able to find what I am missing. Could somebody please help?

rkbrown at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3

Although ServiceProcess exists in .Net 2.0 i can't make it work either.. there is no Class named ServiceController in ServiceProcess namespace.

PanagiotisKefalidis at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
Ops sorry.
ServiceController is there!

eg

System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("Service name");
sc.Start();
sc.Stop();

You just have to add a reference to System.ServiceProcess.dll
I did that, worked fine.

PanagiotisKefalidis at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
Excellent! That is what I was missing.
Thank you.
rkbrown at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6
it's not working on vista
alper k?z?lgil at 2008-1-15 > top of Msdn Tech,Visual C#,Visual C# General...