Question Accessing the Hard Drive Serial and GUID

pizzaboy

Well-known member
Joined
Jan 5, 2008
Messages
57
Programming Experience
1-3
I've been checking endlessly on MSDN and the net in general trying to find out how to get the computer's "C:" drive serial number and also the GUID.

Everything I've found seems to pointing to the System.Management library using the ManagementObject, but "ManagementObject" doesn't appear to be recognized by the IDE?!?

I've imported: System.Management, System.Management.Instrumentation, Microsoft.Win32 and System.IO, but ManagementObject is still coming up as an error.

I'm aware I can simply access the registry to get the GUID, but can anyone simply give me the code to access these 2 elements please...

I'm using VS2010 under Windows 7 64-Bit.


Any help would be greatly appreciated.

Thanks.
 
I get the feeling the bit you're missing is adding the reference to System.Management.
Go to
Project > Add Reference...
Then scroll down until you get to System.Management then select it and press OK.

Then you would need to import System.Management to the class you're trying to use it in so;
VB.NET:
Imports System.Management

Then you should be able to do as you wanted.

One thing to note is that not all HDD's have a serial number, so make sure to check that the serial number is not nothing.

Here's a little snippet of code I just mocked up quickly

VB.NET:
Imports System.Management

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
        Dim wmi_HD As ManagementObject

        For Each wmi_HD In searcher.Get
            If wmi_HD("SerialNumber") IsNot Nothing Then
                MsgBox(wmi_HD("SerialNumber"))
            End If
        Next
    End Sub
End Class

Anyway I hope that this helps

Satal :D
 
You need to add reference to the System.Management.dll library.
 
get the computer's "C:" drive serial number and also the GUID.
"C:" is a logical disk, which is on a partition on a physical disk. See section "...detect which drive letter is associated with a logical disk partition?" here WMI Tasks: Disks and File Systems (Windows) to see example of relationship between these. That code is Vbscript but the WMI queries are still valid.
 
Big whoops there! I always assumed that the Import statement automatically associated the project with the DLL!?!?


Can you please confirm my code is correct?

Dim disk As New ManagementObject("Win32_LogicalDisk.DeviceID=""C:""")
Label1.Text = disk.Properties("VolumeSerialNumber").Value.ToString

Also, is it possible to determine the System Drive, as in the drive Windows is installed, so I can replace "C:"?


I seem to be having a problem retrieving the GUID:

Label2.Text = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGUID")


Are there any instances when these calls will fail? I notice that regedit requires an access confirmation to run under Windows 7?

Cheers guys!
 
Last edited:
Arrrgghh... please help me a little more with this... finding it really difficult to work out the scripting for WMI.

I only need 2 values as part of my security check, so don't want something so small (but so important) to cut further into my development time.

I would like to get the serial number of the harddrive where my program exe is being executed (directory root of Application.ExecutablePath) and also the registry value for "MachineGUID" (under the setting of "HKEY_LOCAL_MACH INE\SOFTWARE\Microsoft\Cryptography").
 
Ok, managed to work out the HD Volume Serial using Satal's example (thanks Satal), but still struggling with the registry reading for the MachineGuid.

I've imported Microsoft.Win32, so why does "Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGuid", "")" return the defaultvalue instead of the reading from the registry? I've checked that it exists through regedit, is there something else that I'm missing?
 
It appears under Windows 7, registry reading requires authorization for obvious security reasons as I predicted earlier.

"Registry.LocalMachine.OpenSubKey("Software\Microsoft\Cryptography", True)" returns a "Requested registry access is not allowed." error.

Apparently this seems to be the case after googling around, any workarounds?
 
Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Mi crosoft\Cryptography", "MachineGuid", "")
This returns the guid in my system (Vista) under a non-privilegied user.
Registry.LocalMachine.OpenSubKey("Software\Micros oft\Cryptography", True)
Here you're requesting write permission, which require admin rights in LocalMachine hive.
 
What a nightmare situation...

Just for reference JohnH, what operating system are you running under?

I'm doing a bit of googling and it appears quite a few people are having problems with reading the registry XP and above.

Another weird thing, on my home PC
Dim path As String = Application.ExecutablePath
Dim drive As String = path.Substring(0, path.IndexOf("\"))
Dim wmiManager As New ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk")
Dim wmiHD As ManagementObject
For Each wmiHD In wmiManager.Get
If (wmiHD("DeviceID") = drive) Then
If wmiHD("VolumeSerialNumber") IsNot Nothing Then result = wmiHD("VolumeSerialNumber")
Exit For
End If
Next

works fine, but on my office PC, debug just locks up at wmiManager.Get line!
 
Back
Top