3

我想在列表框中列出所有连接的网络计算机。有谁知道怎么做?

4

2 回答 2

10

添加对System.DirectoryServices.

添加;

Imports System.DirectoryServices

然后使用;

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
于 2013-03-10T20:51:18.307 回答
0

我砍了一些代码来给你这个,如果它不完美的话应该足够接近(希望它有帮助)......

    Private Sub GetCurrentDevices()

    Try

        Dim ps As New System.Diagnostics.ProcessStartInfo("arp", "-a ")

        ps.RedirectStandardOutput = True
        ps.UseShellExecute = False
        ps.WindowStyle = ProcessWindowStyle.Hidden
        ps.CreateNoWindow = True

        Dim sbResults As New StringBuilder

        Using proc As New System.Diagnostics.Process()

            proc.StartInfo = ps
            proc.Start()

            Dim sr As System.IO.StreamReader = proc.StandardOutput

            While Not proc.HasExited
                System.Threading.Thread.Sleep(100)
            End While

            sbResults.Append(sr.ReadToEnd)

        End Using

        Dim IP_Address, MAC_Address, Device_Name As String

        Dim AllOutputLines() As String = sbResults.ToString.Split(vbCrLf)
        sbResults = Nothing

        For Each IndividualOutputLine As String In AllOutputLines

            Windows.Forms.Application.DoEvents()

            If IndividualOutputLine.Contains("dynamic") Then

                Dim Entries() As String = IndividualOutputLine.Split(New String() {}, StringSplitOptions.RemoveEmptyEntries)

                If Entries.Length = 3 Then

                   'Here is your info ...

                    IP_Address = Entries(0)
                    MAC_Address = Entries(1).ToUpper
                    Device_Name = GetComputerName(IP_Address)


                End If

            End If

        Next

          Catch ex As Exception

        MsgBox(ex.ToString)

    End Try

End Sub

Private Function GetComputerName(ByVal IP_Address As String) As String

    Dim ReturnValue As String = cUnknown

    Try

        Dim myIPs As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(IP_Address)
        ReturnValue = myIPs.HostName
        myIPs = Nothing

    Catch ex As Exception
    End Try

    If ReturnValue = IP_Address Then ReturnValue = cUnknown

    Return ReturnValue

End Function
于 2013-03-10T20:21:20.243 回答