Get And Display Hard Disk Serial Number In VB.NET

This tutorial is all about getting and displaying the hard disk serial number for a complete disk but not in just a single drive using vb.net. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only one textbox named TextBox1 in your Form. 3. Now, lets do the coding. First, we will import the following libraries below to access the hard disk information.
  1. Imports System.Collections
  2. Imports System.Management
Then, we will declare a variable for getting the Thread attribute.
  1. Dim WindowsApplication1 As System.STAThreadAttribute()
Also declare variables for ArrayList, ManagementObjectSearcher, and ManagementObject inside the Form_Load.
  1.         Dim hdCollection As New ArrayList()
  2.         Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
  3.         Dim wmi_HD As New ManagementObject()
Then, we will get the model and the type of the hard drive by using the for each loop with our searcher variable created above.
  1.         For Each wmi_HD In searcher.Get
  2.  
  3.             Dim hd As New Class1.HardDrive()
  4.  
  5.             hd.Model = wmi_HD("Model").ToString()
  6.             hd.Type = wmi_HD("InterfaceType").ToString()
  7.             hdCollection.Add(hd)
  8.         Next
  9.  
  10.  Dim searcher1 As New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
  11.  
  12.  
  13.         Dim i As Integer = 0
  14.         For Each wmi_HD In searcher1.Get()
  15.  
  16.             '// get the hard drive from collection
  17.             '// using index
  18.  
  19.             Dim hd As Class1.HardDrive
  20.             hd = hdCollection(i)
  21.  
  22.  
  23.             '// get the hardware serial no.
  24.             If wmi_HD("SerialNumber") = "" Then
  25.                 hd.serialNo = "None"
  26.             Else
  27.                 hd.serialNo = wmi_HD("SerialNumber").ToString()
  28.                 i += 1
  29.             End If
  30.         Next
Then display all the serial numbers on the textbox by the hdCollection.
  1.         Dim hd1 As Class1.HardDrive
  2.         Dim ii As Integer = 0
  3.  
  4.         For Each hd1 In hdCollection
  5.             ii += 1
  6.        
  7.             TextBox1.Text = TextBox1.Text + "Serial No: " + hd1.serialNo + Chr(13) + Chr(10) + Chr(13) + Chr(10)
  8.         Next

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment