huiselele 发表于 2017-2-12 11:51:51

C#IIS网站应用程序池列表添加修改删除

  //添加应用程序池空间引用
using System.DirectoryServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Management;
  private void button2_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Text="";
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
   foreach(DirectoryEntry a in appPool.Children)
   {    
this.richTextBox1.AppendText(a.Name+"/r");
   }
}
  private void button3_Click(object sender, System.EventArgs e)
{
string AppPoolName=this.textBox1.Text.Trim();
bool ExistAppPoolFlag=false;
  try
{
DirectoryEntry newpool;
   DirectoryEntry apppools=newDirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach(DirectoryEntry a in apppools.Children)
   {    
if(a.Name==AppPoolName)
{
ExistAppPoolFlag=true;
}
   }
if(ExistAppPoolFlag==false)
{
newpool=apppools.Children.Add(AppPoolName, "IIsApplicationPool");   
newpool.CommitChanges();
MessageBox.Show("应用程序池添加成功","添加成功");
}
else
{
MessageBox.Show("应用程序池已经存在","添加失败");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
   
}
  private void button4_Click(object sender, System.EventArgs e)
{
stringoldAppPoolName=this.textBox1.Text.Trim();
stringnewAppPoolName=this.textBox2.Text.Trim();
boolExistAppPoolFlag=false;
  try
{
   DirectoryEntry apppools=newDirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach(DirectoryEntry a in apppools.Children)
   {    
if(a.Name==oldAppPoolName)
{
ExistAppPoolFlag=true;
a.Rename(newAppPoolName);
a.CommitChanges();
MessageBox.Show("应用程序池名称修改成功","修改成功");
}
   }
if(ExistAppPoolFlag==false)
{
MessageBox.Show("应用程序池未找到","修改失败");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
}


private void button5_Click(object sender, System.EventArgs e)
{
stringAppPoolName=this.textBox1.Text.Trim();
boolExistAppPoolFlag=false;
  try
{
   DirectoryEntry apppools=newDirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach(DirectoryEntry a in apppools.Children)
   {    
if(a.Name==AppPoolName)
{
ExistAppPoolFlag=true;
a.DeleteTree();
MessageBox.Show("应用程序池名称删除成功","删除成功");
}
   }
if(ExistAppPoolFlag==false)
{
MessageBox.Show("应用程序池未找到","删除失败");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}

}
  //备注:参考了http://my6521.blog.hexun.com/6602808_d.html的文章
页: [1]
查看完整版本: C#IIS网站应用程序池列表添加修改删除