2013-12-13 4 views
0

나는이 코드를 youtube에서 찾은 것과 똑같습니다. MAC 주소와 NIC 이름은 보여 주지만 IPv4는 표시되지 않습니다. 기본적으로 내 컴퓨터 내부의 모든 네트워크 인터페이스에 ipv4 주소를 표시하고 싶습니다. 여기에 코드VB.net을 사용하여 IPv4 주소를 표시하는 방법

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 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    getinterface() 
End Sub 

이 코드에 대한 해결책이 있습니까 또는 표시의 IPv4 주소를 가진 모든 NIC의 이름을이 작업을 수행하는 또 다른 간단한 방법은 무엇입니까? 현재 Visual Basic Express 2010 Express를 사용 중입니다.

답변

0

WMI 쿼리를 사용하면 훨씬 간단 해집니다.

이러한 유용한 정보를 얻으려면 유용한 유용한 클래스가 있습니다. Win32_NetworkAdapterConfiguration & Win32_NetworkAdapter입니다. WMI 코드를 쉽게 만들려면 WMI Code Creator

Imports System 
Imports System.Management 
Imports System.Windows.Forms 

Namespace WMISample 

    Public Class MyWMIQuery 

     Public Overloads Shared Function Main() As Integer 

      Try 
       Dim searcher As New ManagementObjectSearcher(_ 
        "root\CIMV2", _ 
        "SELECT * FROM Win32_NetworkAdapterConfiguration") 

       For Each queryObj As ManagementObject in searcher.Get() 

        Console.WriteLine("-----------------------------------") 
        Console.WriteLine("Win32_NetworkAdapterConfiguration instance") 
        Console.WriteLine("-----------------------------------") 

        If queryObj("IPAddress") Is Nothing Then 
         Console.WriteLine("IPAddress: {0}", queryObj("IPAddress")) 
        Else 
         Dim arrIPAddress As String() 
         arrIPAddress = queryObj("IPAddress") 
         For Each arrValue As String In arrIPAddress 
          Console.WriteLine("IPAddress: {0}", arrValue) 
         Next 
        End If 
        Console.WriteLine("IPEnabled: {0}", queryObj("IPEnabled")) 
        Console.WriteLine("MACAddress: {0}", queryObj("MACAddress")) 
       Next 
      Catch err As ManagementException 
       MessageBox.Show("An error occurred while querying for WMI data: " & err.Message) 
      End Try 
     End Function 
    End Class 
End Namespace 
을 검색하십시오.
관련 문제