' *****VB2005*****
Option Strict On
Public Class Form1
' add reference to system.management
Private listview1 As New ListView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(listview1)
GetNetworkInfo()
End Sub
Private Sub GetNetworkInfo()
' storing Network adapters in this:
Dim adapters As New ArrayList
Dim pDataCol As System.Management.PropertyDataCollection
Dim pData As System.Management.PropertyData
' Get info from the WIN32_NetworkAdapterConfiguration Class
Dim networkAdapterConfigurationClass As New System.Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim configurationInstances As Management.ManagementObjectCollection = networkAdapterConfigurationClass.GetInstances
Dim configurationInstance As Management.ManagementObject
' Loop through each instance (in this case each adapter)
For Each configurationInstance In configurationInstances
pDataCol = configurationInstance.Properties
pData = pDataCol.Item("IPAddress")
' skip anything without an IP:
If pData.Value Is Nothing Then Continue For
' we are storing the information in a structure called NetworkAdapter defined later...
Dim anotherAdapter As New NetworkAdapter
' There could be more than 1 IP, so the this is an array:
anotherAdapter.IPAddresses = DirectCast(pData.Value, String())
' Get the remaining info for this adapter
' To get the name we need to use another management class, it also has
' the index property of the adapters, so we can find this adapter from its index
pData = pDataCol.Item("Index")
If pData.Value IsNot Nothing Then
' store the index:
anotherAdapter.Index = pData.Value.ToString
' we have a function to get the name:
anotherAdapter.Name = GetName(Convert.ToUInt32(pData.Value))
End If
' finally MACAddress:
pData = pDataCol.Item("MACAddress")
If pData.Value IsNot Nothing Then
anotherAdapter.MACAddress = pData.Value.ToString
End If
' store the NetworkAdapter in the arraylist
adapters.Add(anotherAdapter)
Next
If adapters.Count = 0 Then Exit Sub ' no network adapters!
FillListView(adapters)
End Sub
Private Function GetName(ByVal index As UInteger) As String
' Friendly name is in a different management class
' We'll match adapters with the index
Dim adaptername As String = ""
Dim pDataCol As System.Management.PropertyDataCollection
Dim pData As System.Management.PropertyData
' same as other class really...
Dim networkAdapterClass As New System.Management.ManagementClass("Win32_NetworkAdapter")
Dim networkAdapterInstances As Management.ManagementObjectCollection = networkAdapterClass.GetInstances
Dim networkAdapterInstance As Management.ManagementObject
For Each networkAdapterInstance In networkAdapterInstances
pDataCol = networkAdapterInstance.Properties
pData = pDataCol.Item("Index")
If index = Convert.ToUInt32(pData.Value) Then
' indexes match so this is the same adapter, so get its name
pData = pDataCol.Item("Name")
If pData.Value IsNot Nothing Then
adaptername = pData.Value.ToString
Exit For
End If
End If
Next
Return adaptername
End Function
Private Structure NetworkAdapter
' store info for each adapter:
Public Index As String
Public MACAddress As String
Public IPAddresses() As String
Public Name As String
End Structure
Private Sub FillListView(ByVal adapters As ArrayList)
' display results:
Me.listview1.Dock = DockStyle.Fill
Me.listview1.View = View.Details
Me.listview1.Columns.Add("Name", -1, HorizontalAlignment.Left)
Me.listview1.Columns.Add("IPAddress", -1, HorizontalAlignment.Left)
Me.listview1.Columns.Add("MACAddress", -1, HorizontalAlignment.Left)
For i As Integer = 0 To adapters.Count - 1
Dim adapter As NetworkAdapter = DirectCast(adapters(i), NetworkAdapter)
Dim lvi As New ListViewItem(adapter.Name)
Dim s As String = ""
Dim count As Integer = 0
Do
s &= adapter.IPAddresses(count)
count += 1
Loop Until count = adapter.IPAddresses.Length
lvi.SubItems.Add(s)
lvi.SubItems.Add(adapter.MACAddress)
Me.listview1.Items.Add(lvi)
Next
For i As Integer = 0 To 2
Me.listview1.Columns(i).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)
Next
End Sub
End Class