Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ComponentModel.Win32Exception: Access is denied Error

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();
        }
    }
like image 334
Mahesh Avatar asked Jan 07 '13 10:01

Mahesh


People also ask

What is System ComponentModel Win32Exception?

ComponentModel. Win32Exception is the most basic exception type that will occur within your . NET applications when something goes wrong while using internal win32 -style operating system calls. These can vary from invalid path and file not found errors to network address issues and resource management problems.

What is Win32Exception 0x80004005?

Win32Exception (0x80004005): The system cannot find the path specified. Developer Network.

What is a Win32 exception?

Win32 exceptions are typically exceptions mapped from errors returned from P/Invoked Windows API routines. To be able to debug them properly, you may need to check "Enable native debugging" in project and/or ide settings.


1 Answers

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.

apppool

like image 188
Cybermaxs Avatar answered Sep 21 '22 02:09

Cybermaxs