2013-03-10 164 views
3

我想列出所有連接的網絡計算機列表框中。有誰知道如何?網絡上的計算機名VB.Net

+0

你想看到'計算機的所有計算機 - > Network'頁(Windows 7)? – Neolisk 2013-03-10 02:28:44

+0

是的確切..我不知道從哪裏開始,這是我的問題Dan-o – Kraxed 2013-03-10 14:53:46

回答

9

增加提及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 
+0

如果OP想要所有工作組會怎麼樣?畢竟,「網絡」顯示了一切。 – Neolisk 2013-03-10 22:45:15

+0

查看我的修改後的代碼;-) – PGallagher 2013-03-10 23:13:49

+2

現在您處於正確的軌道上。 Upvoting ... – Neolisk 2013-03-10 23:46:33

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 
+0

感謝您的回覆.. :) – Kraxed 2013-03-11 13:44:59