2

I am trying to develop a windows application to start/stop and monitor status of two particular services.

The problem is I am getting

System.ComponentModel.Win32Exception: Access is denied

Note that both services are local system

The following is my code

private void StartService(string WinServiceName)
{
  ServiceController sc = new ServiceController(WinServiceName,".");
try
{
  if (sc.ServiceName.Equals(WinServiceName))
  {
  //check if service stopped
    if (sc.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Stopped))
    {
       sc.Start();
    }
    else if (sc.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Paused))
    {
        sc.Start();
    }
  }

}
catch (Exception ex)
{
  label3.Text = ex.ToString();
  MessageBox.Show("Could not start " + WinServiceName + "Service.\n Error : " + ex.ToString(), "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
   sc.Close();
   sc.Dispose();
   // CheckStatus();
}
}
IanCian
  • 1,098
  • 2
  • 16
  • 34

1 Answers1

3

Try what leppie suggested in his comment, if it doesn't work you need to tell us which line is throwing exception - when you're creating ServiceController, when you're trying to start it or somewhere else.

Btw, you shouldn't call sc.Start() if the service is paused, you should call sc.Continue().

Also, it is probably better idea to use using construct than try/finally, like this:

private void StartService(string WinServiceName)
{
    try
    {
        using(ServiceController sc = new ServiceController(WinServiceName,"."))
        {
            if (sc.ServiceName.Equals(WinServiceName))
            {
                //check if service stopped
                if (sc.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Stopped))
                {
                   sc.Start();
                }
                else if (sc.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Paused))
                {
                    sc.Continue();
                }
            }
        }
    }
    catch (Exception ex)
    {
        label3.Text = ex.ToString();
        MessageBox.Show("Could not start " + WinServiceName + "Service.\n Error : " + ex.ToString(), "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

That way you don't need to call sc.Close() yourself (btw, you need to call Close only Dispose is redundant - Documentation for Close: Disconnects this ServiceController instance from the service and frees all the resources that the instance allocated.)

EDIT: alt text

Right click on your exe file in Explorer and choose Run As Administrator. In Windows 7, unless you have UAC (User Access Control) turned off you're not running programs as administrator until you explicitly request/or you are asked to do so.

Mihailo
  • 754
  • 3
  • 6
  • hi mihailo, thanks for your suggestions. liked them so far :) the program is giving the error when it comes to sc.Start(); – IanCian Dec 10 '10 at 11:42
  • just to make sure how can I check if I am running under administrator. (i know that i am in administrator account but does not seem that i have full access) – IanCian Dec 10 '10 at 11:44
  • 1
    which sc.Start() ? you have two :) - if you're running your application on Windows 7 or Vista you might have to right click on your app and there should be an option to run it as administrator (it has to do with UAC introduced in Vista) - if you're using Win XP you are running everything as admin if you're in Adminstrators group. – Mihailo Dec 10 '10 at 11:58
  • I am running on Windows 7 and I am talking about the first sc.Start(); – IanCian Dec 10 '10 at 12:02
  • Installed the application and ran the setup as administrator. suddenly it starting working properly. 10x for your help. Still is there a way to avoid this? – IanCian Dec 10 '10 at 12:11
  • I don't know, sorry, there is a way to ask user to run program as Admin but I don't know, I'm not doing Win 7 development I just use Win 7 – Mihailo Dec 10 '10 at 12:14
  • @IanCian: Look at the app.config, there are some comment to allow the app to request elevated privileges. – leppie Dec 10 '10 at 12:22