gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:44:47 24 4
gpt4 key购买 nike

我有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

如果有人能帮助我,我将不胜感激,因为我被这个问题困住了

最佳答案

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

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

关于c# - 从代码安装服务的问题(访问被拒绝?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com