网络 VB.Net 上的计算机名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15318055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:42:06  来源:igfitidea点击:

Computer names on network VB.Net

vb.netlistbox

提问by Kraxed

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

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

回答by PGallagher

Add a reference to System.DirectoryServices.

添加对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

回答by Rob

I chopped up some code to get you this, if its not perfect is should be close enough (hope it helps) ...

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

    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