Windows 7 中的 ServiceController 权限
我有一个由服务和可执行文件组成的应用程序。本质上,它是一个表单应用程序,负责在特定情况下启动和停止服务。
在 Windows XP 上,应用程序使用以下代码可以很好地管理此问题:
ServiceController controller = new ServiceController();
controller.MachineName = ".";
controller.ServiceName = "XXXXXXXXXX";
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
controller.Start();
但在 Windows 7 上,即使我以管理员身份启动了该应用程序,我也会遇到以下异常:
System.InvalidOperationException: Cannot open XXXXXXXXXXXXX service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Start(String[] args)
at System.ServiceProcess.ServiceController.Start()
我可以通过编程方式解决此问题吗?
I have an application which consists of a service and an executable. Essentially it's a forms application that is responsible for starting and stopping a service under specific circumstances.
On Windows XP the application manages this fine using the following code:
ServiceController controller = new ServiceController();
controller.MachineName = ".";
controller.ServiceName = "XXXXXXXXXX";
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
controller.Start();
But on Windows 7, even though I've started the application as an administrator, I get the following exception:
System.InvalidOperationException: Cannot open XXXXXXXXXXXXX service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Start(String[] args)
at System.ServiceProcess.ServiceController.Start()
Is there anything I can do programmatically to resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您说您以管理员身份启动应用程序时,您的意思是在管理员组中的帐户下,还是通过请求管理员凭据的 UAC 提示?如果没有 UAC 凭据提示(或实际上以管理员帐户运行,而不是管理员组中的帐户),您的应用程序没有修改服务的权限,因此您看到的异常是正确的。
这段示例代码可以检查您的应用程序是否以管理员身份运行,如果没有,则启动 UAC 提示。
When you say that you started the application as Administrator, do you mean under an account in the Administrators group, or via a UAC prompt that requests administrator credentials? Without the UAC credentials prompt (or actually running as the Administrator account, not an account within the Administrators group), your application does not have permissions to modify services, so the exception you're seeing is correct.
This bit of example code can check if your application is running as an administrator, and if not, launch a UAC prompt.
仅供参考,如果您不明白为什么它在 Vista 或 7 中不起作用,即使当前用户位于管理员组中,这里是 MSDN 所说的
我记得我第一次使用 7 时感到非常惊讶(我从未使用过 Vista)。
FYI, if you don't understand why it's not working in Vista or 7 even if the current user is in the administrator group, here is what MSDN has to say
I remember I was quite surprised at 1st when using 7 (I never used Vista).
您还可以尝试将应用程序的 UAC 设置为 "在代码中以管理员身份运行”。
You can also try setting the UAC for your application to "Run as Administrator" in code.