Click here to Skip to main content
15,740,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to get HDD Serial Number using
But in Windows 7 Ultimate 64 bit (Service Pack 1) Showing Above Error,
(only one Pc Getting this Error).
other Windows 7/8/10/11 Also Working Fine.
Please Help me how to Solve in this System.

What I have tried:

Public Function HardDiskSerialNumber() As String
        Dim HDD_Serial As String
        Dim hdd As New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
        For Each hd In hdd.Get
            HDD_Serial = hd("SerialNumber")
        Return HDD_Serial.ToString
End Function
Updated 30-Oct-22 9:11am
Richard MacCutchan 30-Oct-22 12:45pm    
You have not shown the line of code that causes the error. However, it should not be too difficult for you to figure out why startindex does not contain a valid value.
Soft Management 30-Oct-22 13:16pm    
But Here is this Error Only.... I am Also Surprised...
When I Am Checking Board Serial No. Using CMD Its Showing Empty
Dave Kreskowiak 30-Oct-22 14:32pm    
Surprised? Why? Your code is assuming there is only ever one "PhysicalMedia" object on the machine. What if there are none? You're code will throw the error you're getting. If there are more, you'll only get one serial number returned, if each PhysicalMedia returned even has a serial number.

You're also assuming that every object that is returned has a serial number. That is not the case with all manufacturers and drives. There is nothing that says every vendor has to support filling in every field of WMI objects, and that includes serial numbers.
Soft Management 1-Nov-22 9:31am    
I am Unable to Get Motherboard and HDD Serial Number using also that Client System.
in my other Clients Working Well my In all Windows OS Like 7/8/10/11 etc

1 solution

First off, HDD_Serial is a string:
Dim HDD_Serial As String
So you don't need to call ToString on it - it does nothing:
Return HDD_Serial.ToString

Secondly, when you assign a value to a string inside a loop, you only get the last value set once the loop is completed.

So you only process the last value returned by the MOS. And if that doesn't have a serial number ... what happens then? We can;t tell what is returned - we don't have access to your specific system. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900