【问题标题】:Issue with installing service from code (access denied?)从代码安装服务的问题(访问被拒绝?)
【发布时间】:2015-06-12 06:52:22
【问题描述】:

我有 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:访问被拒绝):

如果有人可以帮助我,我将不胜感激,因为我对此感到困惑

【问题讨论】:

    标签: c# windows windows-services


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多