c# - 系统.ComponentModel.Win32Exception : Access is denied Error

标签 c# asp.net iis iis-7 iis-7.5

我正在使用 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)。 您可以使用内置帐户或使用您的域之一。

apppool

关于c# - 系统.ComponentModel.Win32Exception : Access is denied Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194146/

相关文章:

asp.net - 增加最大值asp.net 5 (vNext) 中的请求长度

c# - 使用 msbuild 更改连接字符串

c# - 如何在 C# 中停止 ffmpeg 视频录制?

c# - 覆盖文件最快的方法是什么?

asp.net - 我如何将 HTML 渲染到 asp.net 页面?

c# - 在 web.config 中包含数据库密码的潜在安全风险是什么

asp.net - 如何设置 IHttpAsyncHandler 超时?

c# - aspNetCore 2.2.0 - AspNetCoreModuleV2 错误

javascript - 我可以使用JavaScript删除ASP.NET身份验证cookie(.ASPXAUTH)吗?

asp.net - 我们可以使用windows 7家庭版来开发asp.net应用程序吗