2016-09-22 34 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。