以这种方式遍历 DirectoryEntry 对象时出现异常。我的问题是,我应该在哪里做哪些检查来确定是否没有 directoryEntries sites
?
string metabasePath = "IIS://localhost/W3SVC";
DirectoryEntry service = new DirectoryEntry(metabasePath);
DirectoryEntries sites = service.Children;
bool siteExists = false;
foreach (DirectoryEntry directoryEntry in sites)
{
if (directoryEntry.Properties["ServerComment"][0].Equals(SiteName)) //exception thrown here
{
siteExists = true;
break;
}
}
例外
指数超出范围。必须是非负数且小于集合的大小。
参数名称:
System.Collections.CollectionBase.System.Collections.IList.get_Item 处的索引(Int32 索引)