We will assume that a new blank project needs to know the host computers MAC address. This would normally be a complex task, however, we have found a trustworthy source offering a ready-made function to perform the task. I will use this code as an example
Public Shared Function GetMACAddress() As String
Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
Dim MAC As String = String.Empty
For Each mo As ManagementObject In moc
If (MAC.Equals(String.Empty)) Then
If CBool(mo("IPEnabled")) Then
MAC = mo("MacAddress").ToString()
mo.Dispose()
End If
MAC = MAC.Replace(":", String.Empty)
Next
Return MAC
End Function
To keep things simple, we will start from a new, blank Windows Forms Project.
Public Class Form1
End Class
We will now copy/paste our function into the project
Public Class Form1
Public Shared Function GetMACAddress() As String
Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
Dim MAC As String = String.Empty
For Each mo As ManagementObject In moc
If (MAC.Equals(String.Empty)) Then
If CBool(mo("IPEnabled")) Then
MAC = mo("MacAddress").ToString()
mo.Dispose()
End If
MAC = MAC.Replace(":", String.Empty)
Next
Return MAC
End Function
End Class
Most of the functions on this website should work without any further steps.However, in this case, the VB editor will probably show error messages complaining that each reference to "Management" "Is Undefined." You will be unable to build or run the project
An internet search will indicate that the problem lines refer to the "System.Management" namespace. NameSpace problems can often be resolved by adding the correct Imports line. We now add Imports System.Management
Imports System.Management
Public Class Form1
Public Shared Function GetMACAddress() As String
Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
Dim MAC As String = String.Empty
For Each mo As ManagementObject In moc
If (MAC.Equals(String.Empty)) Then
If CBool(mo("IPEnabled")) Then
MAC = mo("MacAddress").ToString()
mo.Dispose()
End If
MAC = MAC.Replace(":", String.Empty)
Next
Return MAC
End Function
End Class
If the error messages disappear, we have solved the problem and our code should work.However, in this case, the VB editor will probably show an additional error messages complaining that the Imports Namespace cannot be found.
The various errors can usually be fixed by Adding a Reference to System.Management. The Following steps explain how to Add References. (It is based on VB.Net 2026, your version of VB.Net may be have slight variations)
- Select "Project" from the menu running across top of VB editor screen
- Select "Add Project Reference" from the drop down menu
- Select "COM" from entries on left
- Use "Search" box to find System.Management
- Click on "System.Management"
- Ensure the System.Management checkbox is ticked
- Click on "OK" button near bottom left
However, in this case, the Search Box is unlikely to find "System.Management," you will have to use the "Cancel" button and your project will still have errors
When VB cannot find the NameSpace, we have to load it from another source. The "NuGet" repository is available from VB.Net and it provides access to many packages. System.Management can be linked to our project using these steps
- Select "Project" from the menu running across top of VB editor screen
- Select "Manage NuGet Packages" from the drop down menu
- Select "Browse" from the top menu of NuGet page
- Use "Search" box to find System.Management
- Click on System.Management text
- Click on the Install button (near top on right side of page)
- If a page appears with Apply and Cancel buttons at bottom right, click on Apply
- Close the NuGet page (use the X on NuGet tab header)
Although this procedure may not resolve every NameSpace problem, it is a good starting point and it often works.
 
Access to System.Management routines will allow you to use various additional functions including
Public Shared Function Get_HD_Serial_No(ByVal strDrive As String) As String
If strDrive = String.Empty OrElse strDrive Is Nothing Then strDrive = "C"
strDrive = Mid(strDrive, 1, 1).ToUpper
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
moHD.[Get]()
Return moHD("VolumeSerialNumber").ToString()
End Function
Public Shared Function Get_Volume_Serial(Optional ByVal strDriveLetter As String = "C") As String
If strDriveLetter = String.Empty Then Return String.Empty
strDriveLetter = Mid(strDriveLetter, 1, 1)
Dim disk As New ManagementObject(String.Format("win32_logicaldisk.deviceid=""{0}:""", strDriveLetter))
disk.Get()
Return disk("VolumeSerialNumber").ToString()
End Function
Public Shared Function Get_Mother_Board_ID() As String
Dim strMotherBoardID As String = String.Empty
Dim query As New SelectQuery("Win32_BaseBoard")
Dim search As New ManagementObjectSearcher(query)
Dim info As ManagementObject
For Each info In search.Get()
strMotherBoardID = info("SerialNumber").ToString()
Next
If strMotherBoardID.Contains("."c) Then
While Mid(strMotherBoardID, 1, 1) = "."
If Len(strMotherBoardID) < 2 Then Exit While
strMotherBoardID = Mid(strMotherBoardID, 2)
End While
End If
Return strMotherBoardID
End Function
When using System.Management based functions you may encounter unexpected behaviour. Examples include
missing/partial values and batches of components with identical serial numbers. Serial numbers have
been used to enforce licencing rules, however, the program should accept 1 or 2 component changes because
some computer repairs require replacement components.
DigitalDan.co.uk