title: LDAP Querying via vb.net
body: <p>I am attempting to write an LDAP query via VB.NET to pull back the atrribute of homeDirectory and throw in in a textbox.  The below code is used to query an LDAP user to check a group theyre a part of.  How can i turn the below in to pull back homeDirectory = textbox1.text.</p>
  
  <pre><code>Private Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
      Dim rootEntry As New DirectoryEntry("fake")
      Dim srch As New DirectorySearcher(rootEntry)
  
      srch.SearchScope = SearchScope.Subtree
      srch.Filter = "(&amp;(objectClass=user)(sAMAccountName=fake)(memberOf=CN=FAKE,OU=Citrix,OU=SecurityGroups,DC=fake,DC=fake))"
      Dim res As SearchResultCollection = srch.FindAll()
  
      If res Is Nothing OrElse res.Count &lt;= 0 Then
          MsgBox("This user is *NOT* member of that group")
      Else
          MsgBox("This user is INDEED a member of that group")
      End If
  End Sub
  </code></pre>
  
  <p>What it looks like now:</p>
  
  <pre><code>Dim rootEntry As New DirectoryEntry("LDAP://johnsmith.com")
  Dim srch As New DirectorySearcher(rootEntry)
  
  srch.SearchScope = SearchScope.Subtree
  srch.Filter = "(&amp;(objectClass=user)(sAMAccountName=jsmith)"
  srch.PropertiesToLoad.Add("homeDirectory")
  
  Dim res As SearchResultCollection = srch.FindAll()
  Dim homeDirectory As String = res(0).Properties("homeDirectory").ToString()
  Dim user As SearchResult = res(0)
  
  If user.Properties.Contains("homeDirectory") Then
      TextBox1.Text = user.Properties("homeDirectory").ToString()
  End If
  
  If res Is Nothing OrElse res.Count &lt;= 0 Then
      MsgBox("This user is *NOT* member of that group")
  Else
      MsgBox("This user is INDEED a member of that group")
  End If
  </code></pre>