C# code //添加應用程序池空間引用 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) { //如果應用程序池不存在,則會報錯系統(tǒng)找不到指定路徑 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) { //如果應用程序池當前狀態(tài)為停止,則會發(fā)生異常報錯 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,"停止失敗"); } }
|
|