2016-09-22 31 views
0

我使用此代碼從AD檢索用戶到Windows窗體上的datagridview。只要沒有任何屬性是空的,它就可以很好地工作。VB.NET DirectorySearcher不會檢索具有空屬性的AD用戶

Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click 
Dim dirEntry As System.DirectoryServices.DirectoryEntry 
     Dim mySearcher As System.DirectoryServices.DirectorySearcher 
     Dim domainName As String = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName 



     Try 
      dirEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" & Environment.UserDomainName) 

      mySearcher = New System.DirectoryServices.DirectorySearcher(dirEntry) 
      mySearcher.Filter = "(samAccountName=" & TextBox8.Text & ")" 

      mySearcher.PropertiesToLoad.Add("samAccountName") 
      mySearcher.PropertiesToLoad.Add("DisplayName") 
      mySearcher.PropertiesToLoad.Add("Description") 
      mySearcher.PropertiesToLoad.Add("mail") 
      mySearcher.PropertiesToLoad.Add("ProfilePath") 
      mySearcher.PropertiesToLoad.Add("HomeDirectory") 
      mySearcher.PropertiesToLoad.Add("HomeDrive") 
      mySearcher.PropertiesToLoad.Add("GivenName") 
      mySearcher.PropertiesToLoad.Add("sn") 

      Dim sr As SearchResult = mySearcher.FindOne() 
      If sr Is Nothing Then 'return false if user isn't found 
       'MsgBox("cannot find") 
      End If 

      Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry() 

      Dim SAM As String = de.Properties("samAccountName").Item(0).ToString 
      If SAM = "" Then 
       SAM = " - " 
      End If 

      Dim DisplayName As String = de.Properties("DisplayName").Item(0).ToString 
      If DisplayName = "" Then 
       DisplayName = " - " 
      End If 

      Dim Description As String = de.Properties("Description").Item(0).ToString 
      If Description = "" Then 
       Description = " - " 
      End If 

      Dim Email As String = de.Properties("mail").Item(0).ToString 
      If Email = "" Then 
       Email = " - " 
      End If 

      Dim Profile As String = de.Properties("ProfilePath").Item(0).ToString 
      If Profile = "" Then 
       Profile = " - " 
      End If 

      Dim HomeDir As String = de.Properties("HomeDirectory").Item(0).ToString 
      If HomeDir = "" Then 
       HomeDir = " - " 
      End If 

      Dim HomeDrv As String = de.Properties("HomeDrive").Item(0).ToString 
      If HomeDrv = "" Then 
       HomeDrv = " - " 
      End If 

      Dim GivenName As String = de.Properties("GivenName").Item(0).ToString 
      If GivenName = "" Then 
       GivenName = " - " 
      End If 

      Dim Sn As String = de.Properties("sn").Item(0).ToString 
      If Sn = "" Then 
       Sn = " - " 
      End If 

      'DataGridView3.Rows.Add(de.Properties("samAccountName").Value.ToString, de.Properties("GivenName").Value.ToString & " " & de.Properties("sn").Value.ToString, de.Properties("ProfilePath").Value.ToString, de.Properties("HomeDirectory").Value.ToString, de.Properties("HomeDrive").Value.ToString, "", "", "", "", "") 
      DataGridView3.AllowUserToAddRows = True 
      DataGridView3.Rows.Add(SAM, DisplayName, Description, Email, Profile, "", HomeDir, "", HomeDrv, "") 
      DataGridView3.AllowUserToAddRows = False 

     Catch 

     End Try 
     dirEntry.Dispose() 
     mySearcher.Dispose() 



    End Sub 

代碼使用的部分:

If SAM = "" Then 
     SAM = " - " 
    End If 

爲打擊這一點,但不工作的嘗試。 如果我在AD中找到用戶並填寫空白字段,用戶將被檢索。

請有人幫助我找出原因以及我可能如何解決它。

有關信息......

我使用Visual Studio 2010

的.Net 3.5 +

非常感謝,

亞倫

回答

0
  1. 據我所知在AD samAccountName不能爲null或空。
  2. 未設置屬性時,它將爲空,但不是「」。
    爲了安全起見,您可以同時檢查「」和null。