Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need the model name of harddisk and its serial number
i could not add the assemblies for class HardDrive (see line A in code)
i have added refrence to System.Management

What I have tried:

private string GetModelAndSerialNumberOfHardDisk()
     {
         string HradDiskModelName, HradDiskSerialNumber;
         ManagementObjectSearcher searcher = null;
         searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

         //DriveInfo[] allDrives = DriveInfo.GetDrives();

         foreach (ManagementObject wmi_HD in searcher.Get())
         {
             HardDrive hd = new HardDrive();//<==== Line A Error
             HradDiskModelName = hd.Caption =
             wmi_HD["Caption"].ToString();
             HradDiskSerialNumber = (hd.SerialNo =
             wmi_HD.GetPropertyValue("SerialNumber").ToString());
             return (HradDiskModelName + "#"+ HradDiskSerialNumber);
         }
         return ("-1");
    }
Posted
Updated 23-Feb-23 0:31am
v4

HardDrive is a class you must provide on your own.
It is not contained in any standard assembly.
Have a look here: How To Get The Serial Number Of Hard Drive By C#[^]
 
Share this answer
 
The following should return the information as selected. you may need to run your application with elevated permissions to access the necessary WMI classes.

using System.Management;

public static void GetModelAndSerialNumberOfHardDisk()
{
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
    foreach (ManagementObject drive in searcher.Get())
    {
        string modelName = (string)drive["Model"];
        string serialNumber = (string)drive["SerialNumber"];
        Console.WriteLine($"Model Name: {modelName}");
        Console.WriteLine($"Serial Number: {serialNumber}");
    }
}


Now simply call the GetModelAndSerialNumberOfHardDisk() method.
 
Share this answer
 
v2
Comments
Richard MacCutchan 23-Feb-23 7:29am    
Off topic (sort of) question. Where can I find a list of the table names used in the SELECT clause?
Andre Oosthuizen 23-Feb-23 10:03am    
Hi Richard, not really off topic...
The ManagementObjectSearcher constructor argument specifies the WMI class to be queried, in this case the Win32_DiskDrive WMI class that contains the information. Some of the properties is -
-Caption
-InterfaceType e.g., IDE, SCSI, USB.
-Manufacturer
-Model
-Partitions
-SerialNumber
-Size
-Status

There are more info such as the number of cylinders, heads, and sectors in the class as well.
More can be found at - Win32_DiskDrive class[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900