logo  VB.Net - Using a Nuget Package
This Chapter
NuGet Packages
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
This page has a slightly different structure to typical "copy and paste" functions used on many other pages. It will provide an introduction to NuGet (a dotNet library of ready made functions.)
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) If you are able to complete the procedure, it is likely to clear the error messages, allowng you to build/run your project
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 The compiler should be able to find System.Management and all the error messages should disappear and You should be able build/run your project.
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