【问题标题】:How to display IPv4 address using VB.net如何使用 VB.net 显示 IPv4 地址
【发布时间】:2013-12-13 13:02:35
【问题描述】:

我在 youtube 上找到了这段代码,并完全按照它进行操作。 MAC 地址和网卡名称显示,但 IPv4 不会显示。基本上我想显示我计算机内所有网络接口的 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

此代码是否有解决方案,或者有另一种更简单的方法来显示所有 nic 名称及其 ipv4 地址?目前使用的是visual basic express 2010 express

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    为此使用 WMI 查询 - 会简单得多。

    有一些有用的类可用 - Win32_NetworkAdapterConfigurationWin32_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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-02-22
      • 1970-01-01
      • 2012-06-27
      • 2012-12-25
      • 2019-08-28
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多