下面的代码将引发异常。我不明白我在代码中犯了什么错误。谁能帮我弄清楚。我认为这是一些担保权问题。如果是这样,我如何将安全权限授予任何用户或应用程序,以便以编程方式访问此windows服务?
Dim sc As New ServiceController
sc.ServiceName = "DataLoad"
If sc.Status = ServiceControllerStatus.Stopped Then
sc.Start()
Else
sc.Stop()
End If异常
System.InvalidOperationException: Cannot open DataLoad 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() at
WEBSITE.DataLoad.Submit1_ServerClick(Object sender, EventArgs e) in C:\Inetpub\wwwroot\WEBSITE\a\DataLoad.aspx.vb:line 46谢谢!
发布于 2011-02-22 22:06:58
为此,可以使用subinacl工具。
SUBINACL /SERVICE \\MachineName\ServiceName /GRANT=[DomainName\]UserName[=Access] 对你的案子要特别注意:
subinacl /service DataLoad /GRANT=YOURDOMAIN\[User in appdomain for WEBSITE]=TO其中TO的意思是
T : Start ServiceO : Stop Service所有可供选择的准入办法如下:
F:全面控制
R : Generic ReadW : Generic WriteX : Generic eXecuteL : Read controLQ : Query Service ConfigurationS : Query Service StatusE : Enumerate Dependent ServicesC : Service Change ConfigurationT : Start ServiceO : Stop ServiceP : Pause/Continue ServiceI : Interrogate ServiceU : Service User-Defined Control Commands 请参阅本文中的方法3
发布于 2013-06-27 21:55:57
我找到了解决此问题的方法,方法是在ServiceController重载构造函数中提供当前正在执行服务的机器名称,该构造函数使用2(2个)参数,即公共ServiceController(/my服务的名称字符串/,System.Environment. machine /此机器正在执行服务/)。
对此解决方案进行测试的.Net版本为4.5,希望这有助于仍在寻找解决方案的任何人。
下面是您需要在代码中做的事情:
ServiceController serviceController = new ServiceController("myServiceName", System.Environment.MachineName);发布于 2013-11-08 13:11:55
GetServiceHandle需要一些访问权限。如果您以管理员用户的身份运行它,而不是作为普通用户运行它,那么本文可能会有所帮助。
它清楚地展示了如何手动授予Windows用户启动和停止服务的权限(或设置其他权限):
http://thommck.wordpress.com/2011/12/02/how-to-allow-non-admins-to-start-and-stop-system-services/
https://stackoverflow.com/questions/5084493
复制相似问题