Computer names on network VB.Net

10,575

Add a reference to System.DirectoryServices.

Add;

Imports System.DirectoryServices

Then use;

Private Delegate Sub UpdateDelegate(ByVal s As String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim t As New Threading.Thread(AddressOf GetNetworkComputers)
    t.IsBackground = True
    t.Start()

End Sub

Private Sub AddListBoxItem(ByVal s As String)
    ListBox1.Items.Add(s)
End Sub

Private Sub GetNetworkComputers()
    Dim alWorkGroups As New ArrayList
    Dim de As New DirectoryEntry

    de.Path = "WinNT:"
    For Each d As DirectoryEntry In de.Children
        If d.SchemaClassName = "Domain" Then alWorkGroups.Add(d.Name)
        d.Dispose()
    Next

    For Each workgroup As String In alWorkGroups

        de.Path = "WinNT://" & workgroup
        For Each d As DirectoryEntry In de.Children

            If d.SchemaClassName = "Computer" Then

                Dim del As UpdateDelegate = AddressOf AddListBoxItem
                Me.Invoke(del, d.Name)

            End If

            d.Dispose()

        Next
    Next
End Sub
Share:
10,575
Kraxed
Author by

Kraxed

VB.Net Enthusiast Learning from Rod Stephens' "Visual Basic 2012 Programmers Reference" & Stack Overflow Would love a job in Software Development Inspiration: http://www.youtube.com/watch?v=oSKDERbPrdQ "Keep calm and code on"

Updated on July 16, 2022

Comments

  • Kraxed
    Kraxed almost 2 years

    I want to list all the connected network computers in a listbox. Does anyone know how?

  • Victor Zakharov
    Victor Zakharov about 11 years
    What if OP wants all workgroups? After all, Network shows everything.