none
Getting Hardisk Serial ID where window is installed.. RRS feed

  • Question

  • Currently we are using this code to get the disk serial no..

     Friend Function GetHDDSerial() As String
            Dim hddsn As String = String.Empty
            Dim query3 As New SelectQuery("Win32_DiskDrive")
            Dim search3 As New ManagementObjectSearcher(query3)
            Dim info3 As ManagementObject
            For Each info3 In search3.Get()
                hddsn = info3("SerialNumber").ToString.Trim
                Exit For
            Next
            Return hddsn
        End Function
     

    this works fine with only 1 issue.

    When any usb or other disk is connected it gives their serial no.

    so all i want to get is a specific serial no of the disk in which windows is installed..

    Thanks in advance..


    Pradeep Yadav (Social MSDN)

    Friday, February 16, 2018 11:17 AM

Answers

  • And below is a WMI based console app to retrieve the serial number of the drive on which Windows is installed that makes no assumptions about the system -

        Sub Main()
            Dim sys As ManagementObject = New ManagementObject("Win32_OperatingSystem=@")
            Dim systemDrive As String = sys("SystemDrive")
            Console.WriteLine("System Drive is {0}", systemDrive)
    
            Dim strQuery1 As String = "ASSOCIATORS OF {Win32_LogicalDisk.DeviceID="""
            Dim strQuery2 As String = """} WHERE AssocClass = Win32_LogicalDiskToPartition"
            Dim strQuery As String = strQuery1 + systemDrive + strQuery2
            Dim relquery As New RelatedObjectQuery(strQuery)
            Dim search As New ManagementObjectSearcher(relquery)
            Dim ndx As UInt32 = 0
            For Each diskPartition In search.Get()
                ndx = diskPartition("DiskIndex")
                Console.WriteLine("Disk Index of System Drive is {0}, Disk Partition is {1}", ndx, diskPartition("DeviceID"))
            Next
    
            Dim serialQuery As SelectQuery = New SelectQuery(String.Format("SELECT * FROM Win32_DiskDrive WHERE Index={0}", ndx))
            Dim serialSearch As ManagementObjectSearcher = New ManagementObjectSearcher(serialQuery)
            For Each ser In serialSearch.Get()
                Console.WriteLine("Serial Number is {0}", ser("SerialNumber"))
            Next
        End Sub
    

    • Marked as answer by pk.yadav Monday, May 28, 2018 7:00 AM
    Monday, February 19, 2018 10:33 AM

All replies

  • You can use DeviceIoControl with the right drive letter

    (I had posted a sample in this thread)


    • Edited by Castorix31 Friday, February 16, 2018 12:06 PM
    Friday, February 16, 2018 12:05 PM
  • Windows would be installed on the first physical drive (e.g., physical drive 0).  So you can use -

            Dim query3 As New SelectQuery("Select * FROM Win32_DiskDrive WHERE Index=0")
    

    Friday, February 16, 2018 12:55 PM
  • Most Windows experts insist that Windows must be installed on the C: but I am not sure. You can find many answers and comments elsewhere indicating that Windows can be booted from other drives/partitions.

    Note that you have an "Exit For" in your code so you are seeing only the first result and that makes the "for" loop unnecessary.

    There are many possibilities in How To Detrmine The Hard Disk which Operating System installed on it ? but also see Win32_BootConfiguration class.



    Sam Hobbs
    SimpleSamples.Info

    Friday, February 16, 2018 9:35 PM
  • For the drive letter, the Shell just uses GetWindowsDirectory
    Friday, February 16, 2018 10:06 PM
  • Windows would be installed on the first physical drive (e.g., physical drive 0).  So you can use -

            Dim query3 As New SelectQuery("Select * FROM Win32_DiskDrive WHERE Index=0")

    This is the output of a WMI Program I made, and it agrees with disk Management:

    0 - Physical Disk 0 - D:\ - D_SEAG_3TB_2012 - IDE - 1 Partition
    1 - Physical Disk 1 - C:\ - C_Local Disk - IDE - 3 Partitions
    2 - Physical Disk 2 - G:\ - G_WD_3TB - IDE - 1 Partition
    3 - Physical Disk 3 - E:\ - Tosh_5TB - IDE - 1 Partition
    4 - Physical Disk 4 - H:\ - H_SEA_3TB_2014 - IDE - 1 Partition
    5 - Physical Disk 5 - I:\ - I_SEA_5TB - SCSI - 1 Partition
    6 - Physical Disk 6 - J:\ - J_My Book-4TB - USB - 1 Partition
    7 - Physical Disk 7 - K:\ - My Book-3TB - USB - 1 Partition
    The bold is my windows drive, and as you can see it is not Physical Drive 0.

    Saturday, February 17, 2018 2:47 AM
  • Something useful when doing WMI is WBEMTEST. Search for it in your system; it is installed in every system that has WMI. When you execute it, click "Connect". Usually we can take the default "root\cimv2"; just click "Connect" a second time. The button to try first is "Enum Instances". Type in a WMI class, such as "Win32_BootConfiguration", then click "OK". Then double-click the result. I get:

    That shows that my boot drive is in Harddisk2. That is because I added a SSD and installed Windows into that.

    You can search for articles about WBEMTEST. It is interesting to click on "Enum Classes..." but be sure to select "Recursive" to see the magic.

    Also try the WMI Explorer.



    Sam Hobbs
    SimpleSamples.Info


    Saturday, February 17, 2018 3:41 AM
  • Windows would be installed on the first physical drive (e.g., physical drive 0).  So you can use -

            Dim query3 As New SelectQuery("Select * FROM Win32_DiskDrive WHERE Index=0")

    This is the output of a WMI Program I made, and it agrees with disk Management:

    0 - Physical Disk 0 - D:\ - D_SEAG_3TB_2012 - IDE - 1 Partition
    1 - Physical Disk 1 - C:\ - C_Local Disk - IDE - 3 Partitions
    2 - Physical Disk 2 - G:\ - G_WD_3TB - IDE - 1 Partition
    3 - Physical Disk 3 - E:\ - Tosh_5TB - IDE - 1 Partition
    4 - Physical Disk 4 - H:\ - H_SEA_3TB_2014 - IDE - 1 Partition
    5 - Physical Disk 5 - I:\ - I_SEA_5TB - SCSI - 1 Partition
    6 - Physical Disk 6 - J:\ - J_My Book-4TB - USB - 1 Partition
    7 - Physical Disk 7 - K:\ - My Book-3TB - USB - 1 Partition
    The bold is my windows drive, and as you can see it is not Physical Drive 0.

    My suggestion about physical disk 0 was in response to the OPs question which indicated that multiple reporting of serial numbers through WMI only occurred when "USB or other disks were connected."  So it seemed reasonable to me that in this case Windows would be on physical disk 0.  No, I did not interrogate the OP as to the ambiguity inherent in the "USB or other disks were connected" comment.

    It was targeted to the OP's circumstance and not suggested to be a solution applicable in all situations. 


    • Edited by RLWA32 Saturday, February 17, 2018 11:24 AM
    Saturday, February 17, 2018 11:18 AM
  • And below is a WMI based console app to retrieve the serial number of the drive on which Windows is installed that makes no assumptions about the system -

        Sub Main()
            Dim sys As ManagementObject = New ManagementObject("Win32_OperatingSystem=@")
            Dim systemDrive As String = sys("SystemDrive")
            Console.WriteLine("System Drive is {0}", systemDrive)
    
            Dim strQuery1 As String = "ASSOCIATORS OF {Win32_LogicalDisk.DeviceID="""
            Dim strQuery2 As String = """} WHERE AssocClass = Win32_LogicalDiskToPartition"
            Dim strQuery As String = strQuery1 + systemDrive + strQuery2
            Dim relquery As New RelatedObjectQuery(strQuery)
            Dim search As New ManagementObjectSearcher(relquery)
            Dim ndx As UInt32 = 0
            For Each diskPartition In search.Get()
                ndx = diskPartition("DiskIndex")
                Console.WriteLine("Disk Index of System Drive is {0}, Disk Partition is {1}", ndx, diskPartition("DeviceID"))
            Next
    
            Dim serialQuery As SelectQuery = New SelectQuery(String.Format("SELECT * FROM Win32_DiskDrive WHERE Index={0}", ndx))
            Dim serialSearch As ManagementObjectSearcher = New ManagementObjectSearcher(serialQuery)
            For Each ser In serialSearch.Get()
                Console.WriteLine("Serial Number is {0}", ser("SerialNumber"))
            Next
        End Sub
    

    • Marked as answer by pk.yadav Monday, May 28, 2018 7:00 AM
    Monday, February 19, 2018 10:33 AM