Riccardo
Riccardo

Reputation: 9

Can't start a service by C#

I have a problem to start a Service by C#. I get this error message:

System.ComponentModel.Win32Exception (0x80004005): Access denied.

How can I have the permission?

My easy code it's:

ServiceController s = new ServiceController("Service1", Environment.MachineName);
try
{
    s.Start();
}
catch (Exception e)
{
    Console.WriteLine(e.InnerException);
}

Upvotes: 1

Views: 240

Answers (1)

Rick
Rick

Reputation: 3451

Like @LasseVågsætherKarlsen said: your application should have sufficient rights and should run elevated. More information here: https://stackoverflow.com/a/2818776/4367

TL;DR;

Add the following to your application manifest:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Upvotes: 1

Related Questions