VB.NET DirectorySearcher无法检索具有空属性

时间:2016-09-22 15:05:27

标签: vb.net directoryservices directorysearcher

我正在使用此代码将用户从AD检索到Windows窗体上的datagridview。只要没有属性为空,它就能很好地工作。

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 +

非常感谢,

亚伦

1 个答案:

答案 0 :(得分:0)

  1. 据我所知,AD samAccountName不能为null或为空。
  2. 当未设置属性时,它将为null,但不是“” 为安全起见,您可以同时检查“”和null。