Issue with installing service from code (access denied?)

169 views Asked by At

I have this code to install windows service from code.

ServiceProcessInstaller ProcesServiceInstaller = new ServiceProcessInstaller();
ProcesServiceInstaller.Account = ServiceAccount.LocalSystem;
//ProcesServiceInstaller.Username = "";
//ProcesServiceInstaller.Password = "";

ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new System.Configuration.Install.InstallContext();
String path = String.Format("/assemblypath={0}", @"C:\Users\g\Documents\Visual Studio 2012\Projects\WindowsService1Test\WindowsService1Test\bin\Debug");
String[] cmdline = { path };

Context = new System.Configuration.Install.InstallContext("", cmdline);
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.DisplayName = "MyService Display name2";
ServiceInstallerObj.Description = "MyService installer test";
ServiceInstallerObj.ServiceName = "MyService2";
ServiceInstallerObj.StartType = ServiceStartMode.Automatic;
ServiceInstallerObj.Parent = ProcesServiceInstaller;

System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
ServiceInstallerObj.Install(state);

The thing is it works. But when I try to run the service I get this message box (Error 5: Access Denied):

enter image description here

I would very much appreciate if someone can help me because I am stuck with this

1

There are 1 answers

0
Jack Miller On

Your service is executed as ServiceAccount.LocalSystem. Probably it does not have permissions to read and execute your compiled file.

Right click containing folder -> permissions -> security -> add SYSTEM -> grant default permissions (at least read and execute).