In addendum to a thread I started earlier, I decided to lookup how one can find the list of computers and domains that exist on the network without using the WMI interface. While the WMI method works, it can take a little while to load.
Thus, here's a much faster method to listing computers and domains in your network.
- Right-Click on your project name in the Solution Explorer, and select "Add Reference..." from the pop up menu.
- In the Dialog that appears, under the .NET tab, search for System.DirectoryServices. Select it and click on "Ok".
- Drag 2 combobox objects onto your form.
- In your code, add the following line above your class definition:
Code:
Imports System.DirectoryServices
- Then in the body of your program, add the following code to the Form_Load event handler (i.e. a subroutine that handles the MyBase.Load event):
Code:
'Determine the list of domains on the network - trusted or not
Dim parent As New DirectoryEntry("WinNT:")
Dim child As DirectoryEntry
Try
For Each child In parent.Children
'If the child element is a domain, add it to the combo box list
If child.SchemaClassName = "Domain" Then
ComboBox1.Items.Add(child.Name)
End If
Next
'Now list all of the computers in the current domain. Notice we used our
'own domain name to filter the results, using the 'Environment' class object
Dim subParent As New DirectoryEntry("WinNT://" & Environment.UserDomainName)
For Each child In subParent.Children
'If the child object is a computername, add it to the second
'combo box
If child.SchemaClassName = "Computer" Then
ComboBox2.Items.Add(child.Name)
End If
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Query Failed")
End Try
The code is lightning fast becasue it's success doesn't rely on executing a WMI query on the domain.
- Tip #1: This code is specific to NT Domains. If you want to use it for an LDAP based domain or network, (eg. ActiveDirectory, or Linux LDAP), simply substitute the text value: "WinNT://" with "LDAP://" and it will work just the same.
- Tip #2: If you like, you could automate the listing of the Computer Names in ComboBox #2 when the user changes the domain name in combo box #1. Simply attach the second part of the code to a subroutine that handles the ComboBox.SelectedIndexChanged event of ComboBox1, then Substitute Environment.UserDomainName with ComboBox1.Text. Remember to do a ComboBox2.Items.Clear() before the rest of the code!
So for those of you implemented the SQLDMO query that I posted on here some time ago, you can provide a Try... Catch block in case that code doesn't work, and in the Catch section of the block, just implement the above code so that at least the end user can see a list of machines with which they can select an appropriate machine.
Enjoy.