We help IT Professionals succeed at work.
Get Started

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

Saxitalis
Saxitalis asked
on
421 Views
Last Modified: 2015-12-26
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"

Problem:

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 ?

Thanks!
Comment
Watch Question
Software Architect
Commented:
This problem has been solved!
Unlock 2 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE