The problem with wmi

I am using this code in the article

http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

but it doesn't work on my machine (russian xp sp3)

what is the problem? i need to get id hdd or motherboard to prevent my program from being copied to other computers.

here is an exception

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

0


source to share


2 answers


According to the class description, the Win32_DiskDrive

properties SerialNumber

and are FirmwareRevision

not available in Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0. This is why you get an exception when you try to access one of them.

You can wrap the code that accesses these properties in a statement try...catch

; something like that:

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

      




Edit: To get the serial number you can try the property Win32_PhysicalMedia.SerialNumber

. 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();
}

      

+3


source


I agree with Helen, but I wouldn't use trying to catch. You should only use this when there are no other alternatives. Have a look at Win32_OperatingSystem for version. If version> = 6.0 then find these properties.



When I researched this in the past, I was unable to find an alternative to the SerialNumber provided in WMI (without using a DLL, which I won't do because my application connects to remote machines).

0


source







All Articles