tags:

views:

121

answers:

1

i use this code in article

http://www.geekpedia.com/tutorial233%5FGetting-Disk-Drive-Information-using-WMI-and-Csharp.html

but it not work on my machine (russian xp sp3)

what the problem? i need to get id hdd or mother board to prevent copy my program on other computers

here is exception

http://www.magicscreenshot.com/jpg/xwMD77wLWEM.html

+1  A: 

As per the Win32_DiskDrive class description, the SerialNumber and FirmwareRevision properties aren't available on Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0. That's why you get an exception when trying to access one of them.

You may want to wrap the code that accesses these properties in a try...catch statement; something like this:

try
{
    lblSerial.Text = "Serial: " + moDisk["SerialNumber"].ToString();
}
catch (ManagementException ex)
{
    lblSerial.Text = "Serial: N/A";
}


Edit: To get the serial number, you could try the Win32_PhysicalMedia.SerialNumber property. Something like this should work:

ManagementObjectSearcher mosRefs = new ManagementObjectSearcher
    ("REFERENCES OF {Win32_DiskDrive.DeviceID='" + moDisk["DeviceID"].ToString() + "'} WHERE ResultClass=Win32_DiskDrivePhysicalMedia");
foreach (ManagementObject moRef in mosRefs.Get())
{
    ManagementObject moMedia = new ManagementObject(moRef["Antecedent"].ToString());
    lblSerial.Text = "Serial: " + moMedia["SerialNumber"].ToString();
}
Helen
and what i must do to get serial number in this OS ? is any solution?
kusanagi
@finnw: It was a typo, it should read `Win32_PhysicalMedia`.
Helen