System.ComponentModel.Win32Exception: Access is denied Error

半世苍凉 提交于 2019-12-17 23:18:16

问题


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

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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/14194146/system-componentmodel-win32exception-access-is-denied-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!