System.ComponentModel.Win32Exception:访问被拒绝错误 [英] System.ComponentModel.Win32Exception: Access is denied Error

查看:6625
本文介绍了System.ComponentModel.Win32Exception:访问被拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#code启动和停止窗口服务,但我收到此错误。

  System.ComponentModel.Win32Exception:访问被拒绝

请帮我。

我的code:

 公共无效StartService(字符串服务名,诠释timeoutMilliseconds)
    {
        ServiceController的服务=新的ServiceController(服务名);
        尝试
        {
            时间跨度超时= TimeSpan.FromMilliseconds(timeoutMilliseconds);            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running,超时);
            lblMessage.Text =服务启动。
        }
        赶上(异常前)
        {
            //lblMessage.Text =服务中的错误启动。
            lblMessage.Text = ex.ToString();
        }
    }


解决方案

请确保您的应用程序池标识帐户,您的服务器上有权限来启动该服务。它的工作原理你的ASP.NET开发服务器上,因为它你的用户帐户下运行(管理员)
在默认的IIS配置,这个帐户是网络服务或ApplicationPoolIdentity(根据IIS版本),通常无法管理服务。

那么,更改IIS管理器中的池帐户(应用程序池/ NameOfYou​​rYourPool /高级设置)。
您可以使用内置的帐户或使用您的域名之一。

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

System.ComponentModel.Win32Exception: Access is denied

Please help me.

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

解决方案

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.

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.

这篇关于System.ComponentModel.Win32Exception:访问被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆