从代码安装服务的问题(拒绝访问?)

时间:2015-06-12 06:52:23

标签: c# windows windows-services

我有this代码从代码安装Windows服务。

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);

事情是有效的。但是当我尝试运行该服务时,我收到此消息框(错误5:访问被拒绝):

enter image description here

如果有人可以帮助我,我会非常感激,因为我坚持这个

1 个答案:

答案 0 :(得分:0)

您的服务以ServiceAccount.LocalSystem执行。可能它没有读取和执行编译文件的权限。

右键单击包含文件夹 - >权限 - >安全 - >添加SYSTEM - >授予默认权限(至少读取和执行)。

相关问题