繁体   English   中英

System.ComponentModel.Win32Exception:访问被拒绝错误

[英]System.ComponentModel.Win32Exception: Access is denied Error

我正在使用 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 配置中,此帐户是网络服务或 ApplicationPoolIdentity(取决于 IIS 版本),通常无法管理服务。

因此,在 IIS 管理器中更改池帐户(应用程序池/NameOfYourPool/高级设置)。 您可以使用内置帐户或使用您的域之一。

应用程序池

在管理员模式下运行你的 VS 并加载你的项目。在管理员模式下打开开发者 VS cmd。给正确的用户名和计算机域名,如域名\\用户名。希望它会起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM