Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get hard disk serial number to generate a licence for my application. after searching on the internet I found this

HERE

But .. using Win32_DiskDrive management object I can get serial number of hard disk but from the list, how can get which hard disk use as primary disk ?
Posted
Updated 30-Apr-15 1:08am
v4

First, in the example you found, I would never rely on the entries in the WMI queries to be always the same order. The WMI queries can have an order by clauses. With that said, that example give you an excellent starting point. What you need to do is get the index of the disk drive for the boot partition. To do that, we need to query Win32_DiskPartition class. In this class, you are looking for the drive index where the BootPartition property is true. The sample code below should help you out.

C#
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach (ManagementObject managementObject in searcher.Get())
{
    Console.WriteLine(managementObject["DeviceId"].ToString());
    Console.WriteLine(managementObject["Index"].ToString());
}

Console.WriteLine();
Console.WriteLine();
Console.WriteLine();

searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskPartition");

foreach (ManagementObject managementObject in searcher.Get())
{
    Console.WriteLine(managementObject["DeviceId"].ToString());
    Console.WriteLine(managementObject["Index"].ToString());
    Console.WriteLine(managementObject["Bootable"].ToString());
    Console.WriteLine(managementObject["BootPartition"].ToString());
}

Console.ReadKey();
 
Share this answer
 
Thanks
VB
virusstorm, But I want to get the serial number for the physical DISK, from your code I can get which DRIVE is boot one, But I still stuck on how can I get the SN for the DISK that have this boo partition ?

Thanks
 
Share this answer
 
Comments
virusstorm 14-May-15 14:11pm    
Please don't post your comments as solutions, otherwise the people on thread don't see that have additional questions. If you combine what I provided with article that you posted in the thread, you will see the solution. You can use the "DeviceId" to locate the physical drive in "Win32_PhysicalMedia" query. I suggest writing a console application that will dump the details to the screen and you will see how the data lines up.

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