我正在使用 C# 代码启动和停止窗口服务,但出现此错误。
System.ComponentModel.Win32Exception: Access is denied
我的代码:
public void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
lblMessage.Text = "Service Started.";
}
catch (Exception ex)
{
//lblMessage.Text = "Error in Service Starting.";
lblMessage.Text = ex.ToString();
}
}
最佳答案
确保您服务器上的应用程序池身份帐户有权启动该服务。它适用于您的 ASP.NET 开发服务器,因为它在您的用户帐户 (admin) 下运行 在默认的 IIS 配置中,此帐户是 Network service 或 ApplicationPoolIdentity(取决于 IIS 版本)并且通常不能管理服务。
因此,更改 IIS 管理器中的池帐户(Application Pools/NameOfYourPool/Advanced Settings)。 您可以使用内置帐户或使用您的域之一。
关于c# - 系统.ComponentModel.Win32Exception : Access is denied Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194146/