2014-09-25 59 views
0

我試圖列出唯一的ACTIVE網絡適配器與其IPv4地址在一臺計算機上。我有這個代碼,但它會列出每個網卡或連接或不連接。列表網絡適配器名稱與IPv4地址

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    ListView1.View = View.Details 
    ListView1.GridLines = True 
    ListView1.FullRowSelect = True 
    ListView1.Columns.Add("Interface Name", 100) 
    ListView1.Columns.Add("MAC address", 100) 
    ListView1.Columns.Add("IPv4 address", 100) 
    ListView1.Columns.Add("Network Mask", 100) 
    ListView1.Columns.Add("IPv6 Address", 100) 
    ListView1.Columns.Add("Link Local Address", 100) 
    ListView1.Columns.Add("IPv5 Address", 100) 
End Sub 

Private Sub getinterface() 
    'get all network interface available in system 
    Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces() 
    If nics.Length < 0 Or nics Is Nothing Then 
     MsgBox("No network interfaces found") 
     Exit Sub 
    End If 


    'if interfaces are found let list them. first clear the listview items 
    ListView1.Items.Clear() 


    For Each netadapter As NetworkInterface In nics 
     'next lets set variable to get interface properties for later use 
     Dim intproperties As IPInterfaceProperties = netadapter.GetIPProperties() 
     'now add the network adaptername to the list 
     ListView1.Items.Add(netadapter.Name) 


     'now get the mac address of this interface 
     Dim paddress As PhysicalAddress = netadapter.GetPhysicalAddress() 
     Dim addbyte As Byte() = paddress.GetAddressBytes() 
     Dim macaddress As String = "" 


     'now loop through the bytes value and change it to hex 
     For i = 0 To addbyte.Length - 1 
      macaddress &= addbyte(i).ToString("X2") 'change string to hex 
      'now let separate hex value with -except last one 
      If i <> addbyte.Length - 1 Then 
       macaddress &= "-" 
      End If 

     Next 

     'ount item in listview 
     Dim icount As Integer = ListView1.Items.Count 

     'use try 
     Try 
      With ListView1.Items(icount - 1).SubItems 
       .Add(macaddress) 
       '.Add(intproperties.UnicastAddresses(2).Address.ToString) 
       .Add(intproperties.AnycastAddresses(2).Address.ToString) 

       .Add(intproperties.UnicastAddresses(2).IPv4Mask.ToString) 

       .Add(intproperties.UnicastAddresses(0).Address.ToString) 
       .Add(intproperties.UnicastAddresses(1).Address.ToString) 
       '.Add(IPAddress.Parse(a).AddressFamily == AddressFamily.InterNetwork) 
      End With 

     Catch ex As Exception 

     End Try 

    Next 
    'now lets make auto size columns 
    ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent) 
End Sub 

有沒有更好的方法來做到這一點?列出唯一連接的具有IPv4地址的網絡適配器。我已經嘗試了WMI代碼編輯器,但不知道要採用哪一種來生成適配器名稱和IP地址

回答

1

下面是我找到的解決方案。

For Each netadapter As NetworkInterface In nics 
     'next lets set variable to get interface properties for later use 
     Dim intproperties As IPInterfaceProperties = netadapter.GetIPProperties() 

     'get first number of IP address. 
     Dim firstnum As String 
     Try 
      firstnum = intproperties.UnicastAddresses(1).Address.ToString() 
      firstnum = firstnum.Substring(0, firstnum.IndexOf(".")) 
     Catch ex As Exception 
      'If not IPv4 then 
      firstnum = "NOPE" 
     End Try 

     'check if first number if valid IPv4 address 
     If Val(firstnum) > 0 And Not Val(firstnum) = 169 And Not Val(firstnum) = 127 Then 


      'now add the network adaptername to the list 
      ListView1.Items.Add(netadapter.Name) 
0

使用netadapter.OperationalStatus == OperationalStatus.Up選擇活動的適配器。

(對不起,這是C#,但VB中的等價物應該很容易。)