By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

Retrieve disk serial number

P: n/a
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
Sep 25 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Steve Barnett wrote:
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

Sep 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.