Retrieve disk serial number

S

Steve Barnett

I'm using the following code to retrieve the disk serial number of my hard
disk, but have nagging doubts as to whether it'll work on other machines.
Does anyone have experience of doing this who can advise whether I'm on
relatively safe ground here:

using System.Management;
....
private string GetDiskSerial()
{
string strSerial = "";

SelectQuery selQuery = new SelectQuery("SELECT * FROM
WIN32_DiskDrive");
ManagementObjectSearcher searchResult = new
ManagementObjectSearcher(selQuery);

foreach (ManagementObject item in searchResult.Get())
{
if (item["Signature"] != null)
{
strSerial = item["Signature"].ToString();
break;
}
}

return strSerial;
}

What I'm not sure of is the order in which disks are presented, so what
happens if there is more than one physical disk drive and what happens if
this is a diskless workstation.

I know I get the right result on my machine as the value returned matches
the one I see in the WMI browser.

Thanks
Steve
 
C

Clint (cmueller

Steve said:
I'm using the following code to retrieve the disk serial number of my hard
disk, but have nagging doubts as to whether it'll work on other machines.
Does anyone have experience of doing this who can advise whether I'm on
relatively safe ground here:

using System.Management;
...
private string GetDiskSerial()
{
string strSerial = "";

SelectQuery selQuery = new SelectQuery("SELECT * FROM
WIN32_DiskDrive");
ManagementObjectSearcher searchResult = new
ManagementObjectSearcher(selQuery);

foreach (ManagementObject item in searchResult.Get())
{
if (item["Signature"] != null)
{
strSerial = item["Signature"].ToString();
break;
}
}

return strSerial;
}

What I'm not sure of is the order in which disks are presented, so what
happens if there is more than one physical disk drive and what happens if
this is a diskless workstation.

I know I get the right result on my machine as the value returned matches
the one I see in the WMI browser.

Thanks
Steve

Steve,

I'm using Win32_LogicalDisk, so the results may be different. When I
loop through the objects in my ManagementObjectCollection, the results
are brought back in order of the drive letter assigned to it, so C:\,
D:\, etc. My guess is that Win32_LogicalDisk returns it in order of
partition/disk ID.

I would suspect that Win32_DiskDrive would do the same thing.

Hope that helps,
Clint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top