日期:2014-05-20  浏览次数:20912 次

写了一个删除iis应用程序池的类,单个删除可以,批量删除出现问题
类如下,如果一个一个删除则是可以的,如果循环所有的应用程序池来调用删除,出现下面的奇怪情况:
假设有多个池:1,2,3,4,5,6,7,8,9
第一次循环,只删除1,3,5,7,9
第二次循环,只删除余下的2,6
第三次循环,只删除余下的2
第四次删除,4
我能确定池都是空的,而且都可以删除的,在使用循环的时候,如果某个池不删除,则调用这个类之后的程序也不会执行,请教一下是什么原因.

类如下:

///   <summary>
///   删除应用程序池
///   </summary>
///   <returns> </returns>
public   bool   DeleteAppPoolByName(string   AppPoolName)
{
bool   RetStr=false;

try
{
DirectoryEntry   IISEntryPool=new   DirectoryEntry( "IIS://localhost/w3svc/AppPools ");
foreach(DirectoryEntry   de   in   IISEntryPool.Children)      
{
if(String.Compare(de.Name,AppPoolName,true)==0)
{
IISEntryPool.Children.Remove(de);
IISEntryPool.CommitChanges();
}
}
}
catch(Exception   ex)
{
//错误处理过程
}
return   RetStr;
}


------解决方案--------------------
up
------解决方案--------------------
试试这样

public bool DeleteAppPoolByName(string AppPoolName)
{
bool RetStr=false;


DirectoryEntry IISEntryPool=new DirectoryEntry( "IIS://localhost/w3svc/AppPools ");
foreach(DirectoryEntry de in IISEntryPool.Children)
{
if(String.Compare(de.Name,AppPoolName,true)==0)
{
try
{
IISEntryPool.Children.Remove(de);
IISEntryPool.CommitChanges();
}
catch(Exception ex)
{
//错误处理过程
}

}
}
return RetStr;
}

------解决方案--------------------
for i --
------解决方案--------------------
try{}
catch(Exception ex){}
------解决方案--------------------
IISEntryPool.CommitChanges();
放 到 循环 外

因为 IISEntryPool.CommitChanges() 影响 了 循环
------解决方案--------------------
你自己用ArrayList测试一下就知道原因了,因为实现了IEnumerater的集合不能在foreach语句中删除
------解决方案--------------------
我 说错了
greennetboy(我的老婆叫静静) 说的 对
是 foreach 的 问题
------解决方案--------------------
public bool DeleteAppPoolByName(string AppPoolName)
{
bool RetStr = false;

try
{
DirectoryEntry IISEntryPool = new DirectoryEntry( "IIS://localhost/w3svc/AppPools ");

ArrayList tmplist = new ArrayList();
foreach (DirectoryEntry de in IISEntryPool.Children)
{
if (String.Compare(de.Name, AppPoolName, true) == 0)
{
tmplist.Add(de);
}
}

for (int i = 0; i < tmplist.Count; i++)
{
IISEntryPool.Children.Remove((DirectoryEntry)tmplist[i]);
}

IISEntryPool.CommitChanges();

}
catch (Exception ex)
{
//错误处理过程
}
return RetStr;
}
------解决方案--------------------