1

전자 메일 주소와 함께 전체 사용자 목록을 얻으려고합니다.DirectoryServices 검색 범위 초과 오류

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll 

아무도 이런 일이 어떻게이 문제를 방지하는 이유를 알고 있습니까 : 많은 것들을 시도 후에 아래 마침내 그러나 나는이 오류이 오류가 나에게 기쁨의 어떤 형태를 준있다? 전체 코드는 다음과 같습니다. 이 res.Properties를 가정 한 것입니다처럼 보이는

Dim entry As DirectoryEntry = Nothing 
Dim search As DirectorySearcher = Nothing 
entry = New DirectoryEntry() 
search = New DirectorySearcher() 
search.Filter = "(&(objectCategory=person)(objectClass=user)(mail=*@companyname.com*))" 
search.Sort.PropertyName = "cn" 
Dim result As SearchResultCollection = search.FindAll() 
For Each res As SearchResult In result 
    Dim Name = res.Properties("cn")(0).ToString() 
    Dim Email = res.Properties("mail")(0).ToString() 
WindowsForm1.ListBox1.Items.Add(Name & " <" & Email & ">") 
Next 
entry.Dispose() 
search.Dispose() 
result.Dispose() 

답변

0

그들에 적어도 하나 개의 요소가 배열 인 값으로 키 "CN"와 "메일"을 가지고있다.

res.Properties ("CN") (0)로 .toString()

이 그 키 이름 문자열로 CN 인 res.Properties에서 어레이의 첫 번째 요소를 변환 '라는

. ' 그런 종류의 소리는 혼란 스럽습니다. 그리고 그것은 당신이을 알고 가정

  1. res.Properties 누구의 키 이름 즉 요소가 배열
  2. 의 한 유형 인 값을 가지고
  3. CN 그 배열 요소가있는 요소가 위치에있는 요소를 문자열로 변환 할 수있는 위치에 0
  4. 에서이 글은

은 시도하기 전에 다음을 확인하십시오 그들을 액세스 할 수 있습니다. 특정 유형의 기능을 살펴 보지 않았지만 아래에서 작동해야합니다.

Dim Name, Email as String 
If Not IsNothing(res.Properties("cn")) AndAlso res.Properties("cn").Count > 0 AndAlso Not IsNothing(res.Properties("mail")) AndAlso res.Properties("mail").Count > 0 Then 

    Name = res.Properties("cn")(0) 
    Email = res.Properties("mail")(0) 
End If 

이 더 정리해야하지만, 아이디어와 근본 원인은 동일합니다 - 우리가 액세스 할 값을 가진 배열을 가지고 있는지 때까지 우리가 배열의 값에 접근 피하기 위해 노력하고 처음에는 항상 데이터의 유효성을 확인하십시오.