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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14194146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:01:34  来源:igfitidea点击:

System.ComponentModel.Win32Exception: Access is denied Error

c#asp.netiisiis-7iis-7.5

提问by Mahesh

I am using C# code to start and stop the window serves but I am getting this error.

我正在使用 C# 代码来启动和停止窗口服务,但出现此错误。

System.ComponentModel.Win32Exception: Access is denied

My code:

我的代码:

 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();
        }
    }

回答by Cybermaxs

Make sure your application pool identity account on your server has permissions to start that service. It works on your ASP.NET Development Server because it runs under your user account (admin) In a default IIS configuration, this account is Network service or ApplicationPoolIdentity (depending on IIS version) and usually cannot manage services.

确保您服务器上的应用程序池身份帐户有权启动该服务。它适用于您的 ASP.NET 开发服务器,因为它在您的用户帐户 (admin) 下运行。在默认的 IIS 配置中,此帐户是网络服务或 ApplicationPoolIdentity(取决于 IIS 版本),通常无法管理服务。

So, change the pool account in IIS Manager (Application Pools/NameOfYourYourPool/Advanced Settings). You can use a built-in account or use one of your domain.

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

apppool

应用程序池

回答by priyesh jaiswal

Run your VS in administrator mode and load your project.Open developer VS cmd in administrator mode.Give proper username with computer domain name like domainname\username.Hope it will work.

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