title: STOP Application Pool Issue Programmatically IIS7
body: <p>In my application I needs to stop a application pool programmatically in IIS 7. I have created a local account and given him Administrative Priviledges. Here is the code</p>
  
  <pre><code>Private static void StopApplication()
  {
      string serviceHostDeploymentType = "local";
          if (serviceHostDeploymentType.Equals("local"))
          {
              WindowsIdentityImpersonate newIdentity = new WindowsIdentityImpersonate();
              if (newIdentity.Impersonate("AccountName", Environment.MachineName, "Password"))
              {
                  try
                  {
                      ServerManager serverManager = ServerManager.OpenRemote("Server");
                      string siteName = GetWebSiteNameById(serverManager, 1);
                      Site parentWebsite = serverManager.Sites[siteName];
  
                      if (parentWebsite != null)
                      {
                          ApplicationPool apppool = serverManager.ApplicationPools["Application Pool Name"];
  
                          if (apppool != null)
                          {
                              apppool.Stop();
                          }
                      }
                  }
                  finally
                  {
                      newIdentity.Undo();
                  }
              }
          }
          else
          {
             throw new ConfigurationErrorsException("...");
          }
      }
  </code></pre>
  
  <p>I am getting an error Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).</p>
  
  <pre><code>Stack Trace
  
      at Microsoft.Web.Administration.Interop.IAppHostMethodInstance.Execute()
         at Microsoft.Web.Administration.ConfigurationElement.ExecuteMethod(String methodName)
         at Microsoft.Web.Administration.ApplicationPool.Stop()
         at ServerManagerTesting.Form1.StopApplication()
         at ServerManagerTesting.Form1.button1_Click(Object sender, EventArgs e)
         at System.Windows.Forms.Control.OnClick(EventArgs e)
         at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
         at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
         at System.Windows.Forms.Button.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
         at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
         at ServerManagerTesting.Program.Main()
         at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>