title: Access Denied while trying to stop a C# Windows Service
body: <p>I have created a C# web service using visual studio to stop the windows service 'Simple Mail Transfer Protocol (SMTP)' called SMTPSVC.</p>
  
  <p>The following is the web method to do it:</p>
  
  <pre><code>[WebMethod]
  public string StopService()
  {
      String status = "";
      try
      {
          ServiceController scAlerter = new ServiceController("SMTPSVC");
  
          Console.WriteLine(scAlerter.DisplayName);
          Console.WriteLine(scAlerter.CanStop);
  
          scAlerter.Stop();
          Console.WriteLine("Service stopped");
          status = "STOPPED";
      }
      catch (Exception e)
      {
          Console.WriteLine("Exception caught here" + e.ToString());
          status = "Exception" + e.ToString();
      }
      return status;
  }
  </code></pre>
  
  <p>I published this web service in my IIS 5.1 server. When I invoked the service it is throwing the following 'Access Denied' exception</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; 
  &lt;string xmlns="http://y-square.org/"&gt;
      ExceptionSystem.InvalidOperationException: Cannot open SMTPSVC service on 
      computer '.'. ---&gt; System.ComponentModel.Win32Exception: Access is denied 
      --- End of inner exception stack trace --- at 
      System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
      at System.ServiceProcess.ServiceController.Stop() at Service.RestartService()
      in c:\Inetpub\wwwroot\RestartService\App_Code\Service.cs:line 38
  &lt;/string&gt; 
  </code></pre>
  
  <p>By default the service is using the user account <code>IUSER_system-name</code> and I have added this user account into system Administrators group and also added <code>ASPNET</code> user account in Administrator group.</p>
  
  <p>I was able to stop/start this windows service from C# standalone program successfully. </p>
  
  <p>Can you kindly let me know what is the problem? Any permission settings or IIS user access rights shall I need to change in order to run this?</p>
  
  <p>Also let me know which user account this C# service would use to stop the Windows Service?</p>
  
  <p>Your help is much appreciated.</p>
  
  <p>Thanks in advance,
  Yogi</p>