1. 程式人生 > >C#IIS網站應用程式池啟動回收停止

C#IIS網站應用程式池啟動回收停止

//新增應用程式池空間引用
using System.DirectoryServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Management;

private void button6_Click(object sender, System.EventArgs e)
  {
   //如果應用程式池不存在,則會報錯系統找不到指定路徑
   string AppPoolName=this.textBox1.Text.Trim();
   string method="Start";

   try
   {    
      DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
      DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool");
      findPool.Invoke(method,null);
      appPool.CommitChanges();
      appPool.Close();
    MessageBox.Show("應用程式池名稱啟動成功","啟動成功"); 
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,"啟動失敗");      
   }

  }

  private void button7_Click(object sender, System.EventArgs e)
  {
   //如果應用程式池當前狀態為停止,則會發生異常報錯
   string AppPoolName=this.textBox1.Text.Trim();
   string method="Recycle";

   try
   {    
      DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
      DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool");
      findPool.Invoke(method,null);
      appPool.CommitChanges();
      appPool.Close();
    MessageBox.Show("應用程式池名稱回收成功","回收成功"); 
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,"回收失敗");      
   }  
  }

  private void button8_Click(object sender, System.EventArgs e)
  {
   string AppPoolName=this.textBox1.Text.Trim();
   string method="Stop";

   try
   {    
      DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
      DirectoryEntry findPool = appPool.Children.Find(AppPoolName,"IIsApplicationPool");
      findPool.Invoke(method,null);
      appPool.CommitChanges();
      appPool.Close();
    MessageBox.Show("應用程式池名稱停止成功","停止成功"); 
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,"停止失敗");      
   }  
  }