Start/Stop/Restart a windows Service from C# with ServiceController

Saxitalis used Ask the Experts™
Hello Experts,

I want to restart the "SAVService" (Sophos Antivirus) windows service using the ServiceController class in C#.

-- I added a reference to the ServiceProcess.dll in my C# project

-- I defined the service like this:
myService.ServiceName = "SAVService";

--I can get the status of the service like this
string svcStatus = myService.Status.ToString(); //This works - Shows SAVService is "Runing"


When I try to stop the SAVService like this: myService.Stop();

I get an Exception "Cannot open SAVService on the computer" I suspect this may be a security issue. The SAVService on my computer will only start/stop using the Login "Local System" or Login/pswd "Local Service\Sophos default password"  

Does anyone know how to define the User/Password for a windows service in   ServiceController ?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Karrtik IyerSoftware Architect
If you plan to use Service Controller then you will have to use WMI to set the user name and password while starting the service.(WMI)
You can also use service manage command line options to invoke this command from C# code.
I would suggest if possible and feasible, you use PsService utility from Microsoft/SysInternals that provide command line arguments to pass user name and password while starting service. You can invoke this from the C# code. Please refer the link below for the examples on command line arguments to this PsService Utility.


Thanks, -I'll look into SCM and PSService. I actually need to do this on remote computers in a Windows 7 work group.

Will these tools apply to remote computer service start/stop?
Software Architect
Yes PSService allows to you start/stop services on remote computer as well. Please refer example below on how that can be done.


Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial