从 Windows 服务关闭网络电脑时出现问题
我正在尝试从 ac# windows 服务关闭网络上的远程计算机。当我在测试应用程序中运行下面的代码时,它工作正常,但是当它从服务运行时,什么也没有发生。
我感觉这可能与权限有关,但不确定。
有人有什么建议吗?
Process p = new Process();
p.StartInfo.FileName = "shutdown";
p.StartInfo.Arguments = @"/s /f /m \\pc-name /t 0";
p.Start();
i am trying to shutdown a remote computer on my network from a c# windows service. When i run the code below in a test app it works fine but when its run from the service nothing happens.
I have a feeling it may have something to do with permissions but not sure.
Does anyone have any suggestions?
Process p = new Process();
p.StartInfo.FileName = "shutdown";
p.StartInfo.Arguments = @"/s /f /m \\pc-name /t 0";
p.Start();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该服务通常使用测试应用程序以外的其他凭据(本地系统、网络服务等)运行。您应该考虑创建一个具有适当权限的域用户并将该用户添加到您的服务中。
The service normaly runs with other credentials (Local System, Network Service etc) than your test app. You should consider creating a domain user that has the proper rights and add that user to your service.
如果您在远程电脑上登录的帐户没有 SE_SHUTDOWN_NAME 权限,关机将会失败。
此MSDN 文章包含一个代码片段,向您展示如何从程序设置 SE_SHUTDOWN_NAME 权限(以及如何使用 Windows API 关闭/重新启动 PC,而不是调用 shutdown 命令)。
但由于您是远程运行,并且可能无法以编程方式设置这些权限,因此您可以通过交互式登录到远程计算机来实现此目的:
您只需执行一次。
祝你好运。
shutdown will fail if the account that you are logging in to on the remote PC doesn't have the SE_SHUTDOWN_NAME privilege.
This MSDN article contains a code snippet showing you how to set the SE_SHUTDOWN_NAME privilege from a program (and how to shutdown/restart the PC using the Windows API instead of calling the shutdown command).
But since you are running remotely and you may not be able to set those permissions programmatically, you can do so by logging in to the remote machine interactively and:
You will only have to do that once.
Good luck.