2013-09-03 13 views
0

이 방식으로 DirectoryEntry 객체를 반복 할 때 예외가 발생합니다. 내 질문은 어떤 체크가 어디에 sites에 디렉토리 엔트리가 없는지를 결정하기 위해 어디에서해야합니까?DirectoryEntry를 반복 할 때 예외가 발생합니다.

 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 지수)에서이

+0

예외가 분명히 말합니다. 'Properties [ "ServerComment"] [0]'요소가 없으므로 예외가 발생합니다. –

+0

@marc_s 널 포인터 예외가 아닙니다. 추가 할 필요가있는 체크는'directoryEntry.Properties [ "ServerComment"]입니다. Count> 0' – invertigo

답변

2

이가 인 경우 문제가 여기

directoryEntry.Properties["ServerComment"][0] 

것 같다 이 여분의 유효성 검사는 트릭을 수행해야합니다.

if (directoryEntry.Properties["ServerComment"] != null && 
    directoryEntry.Properties["ServerComment"].Count > 0 && 
    directoryEntry.Properties["ServerComment"][0].Equals(SiteName)) 
관련 문제