C# Windows,启动服务系统。InvalidOperationException:无法在计算机“”上启动服务。访问被拒绝(以管理员身份运行)

C# Windows,启动服务系统。InvalidOperationException:无法在计算机“”上启动服务。访问被拒绝(以管理员身份运行),c#,windows,windows-services,C#,Windows,Windows Services,我正在尝试使用以下代码启动服务。这在99%的机器上运行良好,但我在用户机器上遇到了这个问题。任何有助于重现此错误或此问题发生的原因的帮助 ServiceController sc = new ServiceController(name); if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerSt

我正在尝试使用以下代码启动服务。这在99%的机器上运行良好,但我在用户机器上遇到了这个问题。任何有助于重现此错误或此问题发生的原因的帮助

            ServiceController sc = new ServiceController(name);

            if (sc.Status == ServiceControllerStatus.Running ||
                    sc.Status == ServiceControllerStatus.StartPending)
            {
                sc.WaitForStatus(ServiceControllerStatus.Running);
                Logger.Info("Service already running");
                return true;
            }
            sc.Start();
我收到的错误是

System.InvalidOperationException:无法在计算机上启动服务 '.'. --> System.ComponentModel.Win32异常:访问被拒绝

我正在以管理员权限运行


在创建服务时,我还运行sc sdset命令,使服务由非管理员进程启动和停止。

中有一个完整的线程。该问题对许多用户持续存在,您似乎没有足够的权限启动该服务,在这种情况下,您必须将该服务更改为管理帐户:

确保通过以下方式将服务设置为本地帐户:

右键单击Services.msc面板中的属性。 选择登录选项
然后再次检查它是否工作。

我假设您正在尝试实现类似的场景,即安装服务并自动启动它

假设您已确保确实以管理员身份运行,即,在Admin命令提示符下或以管理员身份运行

此外,假设您已重新启动计算机,以确保服务的旧版本确实已删除,因为您已多次尝试安装/卸载服务

错误访问被拒绝本质上意味着运行它的用户没有访问权限。因为您已经说过您是以管理员身份运行的,所以可能是管理员没有启动服务的权限。可能您正处于一个锁定的环境中,可能是通过组策略,其中只有域管理员是最强大的

下一步是调查您的服务拥有的权限。一个有用的工具是:SubInAcl

显示或修改文件和文件夹权限、所有权和域的访问控制条目ACE

SubInAcl/服务您的服务名称

上面的命令不容易使用!您需要从Microsoft网站下载它

另一个有用的工具是SC。默认情况下,这通常是可用的

服务控制-创建、启动、停止、查询或删除任何Windows服务

请显示您的服务名称

将提供许可的详细信息

这将为您提供有助于进一步调查的数据

您还可以使用此命令启动/停止服务。您可以尝试使用此工具来检查在使用此工具时是否也遇到相同的异常

下面的serverfault问题提供了有关设置服务权限的一些详细信息

如果要查看installutil中的堆栈跟踪,可以使用/ShowcallStack选项

/ShowCallStack 如果在安装过程中的任何时候发生异常,则将调用堆栈输出到日志文件


这不是答案,而是我对这个问题的进一步探索

这是日志

Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the 

D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.

An exception occurred during the Install phase.
System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of Flight.ServiceHost.Installation.
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at Flight.ServiceHost.Installation.Install(IDictionary stateSaver) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 36
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.Configuration.Install.AssemblyInstaller.Install(IDictionary savedState)
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.Configuration.Install.TransactedInstaller.Install(IDictionary savedState)
The inner exception System.InvalidOperationException was thrown with the following error message: Cannot start service PreFlight on computer '.'..
   at System.ServiceProcess.ServiceController.Start(String[] args)
   at System.ServiceProcess.ServiceController.Start()
   at Flight.ServiceHost.Installation.OnAfterInstall(IDictionary savedState) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 49
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: Access is denied.


The Rollback phase of the installation is beginning.
See the contents of the log file for the D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.

The Rollback phase completed successfully.

The transacted install has completed.
[更新]

我应该提到我正在运行WIndows 10
我成功地使用创建了一个安装程序,它确实起作用。

我的错误发生在OnAfterInstall事件中。var sc=新ServiceControllersi.ServiceName;如果sc.Status==ServiceControllerStatus.Stopped{sc.Start;}@kirstengride您可以发布堆栈跟踪吗?我正在使用命令installutil.exe myservice.exe如何获取堆栈跟踪?我创建了一个答案,以便更好地格式化我发现的内容。您还可以添加日志文件的内容吗?D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\Flight.ServiceHost.InstallLogIt相同。