Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developed an application that use hard disk serial number for activation but when its comes to pc that uses multiple hardisks its being a problem so iam trying to find the serial number of hard disk which comes with the os instalation more clearly the hard disk that act as C drive .I also tried the below code but it returns D drive information sometime.

What I have tried:

public string GetHardSerial()
      {
          ManagementObjectSearcher Finder = new ManagementObjectSearcher("Select * from Win32_OperatingSystem");
          string Name = "";
          string SerialNumber = "";
          foreach (ManagementObject OS in Finder.Get()) Name = OS["Name"].ToString();

          //Name = "Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\Partition1"

          int ind = Name.IndexOf("Harddisk") + 8;
          int HardIndex = Convert.ToInt16(Name.Substring(ind, 1));
          Finder = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Index=" + HardIndex);
          foreach (ManagementObject HardDisks in Finder.Get())
              foreach (ManagementObject HardDisk in HardDisks.GetRelated("Win32_PhysicalMedia"))
                  SerialNumber = HardDisk["SerialNumber"].ToString();
         // TextBox1.Text = Convert.ToString(Name);
         // TextBox2.Text = Convert.ToString(SerialNumber);
          return SerialNumber;

      }
Posted
Updated 16-May-19 3:06am
Comments
Kornfeld Eliyahu Peter 16-May-19 6:21am    
What is the problem? You do not know how to find the hard disk that OS installed on? Or you do not know how to get the serial for that disk?
Member 14052128 16-May-19 6:34am    
exactly how do i find the serial key for hard disk with os installed?
Member 14052128 16-May-19 6:35am    
the code i have tried will return the serial key for which comes the first drive as orderd in device manger
F-ES Sitecore 16-May-19 7:10am    
Just so you know, this is getting the drive details of the server your app is installed on, not the client browser.

Windows is not always installed on C: drive. More than that, C: does not represent a disk, it represents a partition on a disk.
If your code gets the serial number of the D: drive, it means that Windows in installed on the partition which has been assigned the letter D.
Why is it an issue since you only need the result to be unique for a single computer? Whatever serial is returned, you only need the same serial to be returned everytime the function is called on the same computer.
 
Share this answer
 
Comments
Member 14052128 16-May-19 6:28am    
the problem comes when if iam connecting an external hardrive it will take its serial number.
Kornfeld Eliyahu Peter 16-May-19 9:03am    
How that come - by nature serial numbers are unique (it includes a manufacturer id and an internal serial)...
phil.o 16-May-19 9:46am    
Why do you think so? If you plug an external drive, it will be assigned an index which is different from that of system drive. And your code will still get the drive with the right index.
Maciej Los 16-May-19 6:46am    
5ed!
phil.o 16-May-19 7:37am    
Thanks.
Rather than the boot up hard drive, you might be better handling the processor serial number instead: HDDs have a limited life, and they fail a lot quicker than processors do. A restored image from a backup would not work in your system, where changing the processor is almost certainly a new or different computer (and probably new RAM and motherboard at the very least).
These may help:
How to get PC serial number using C#[^]
Processor serial number[^]
 
Share this answer
 
If you would like to get drive letter where Windows OS is installed, use this: Environment.GetEnvironmentVariable Method (System) | Microsoft Docs[^]

C#
string windir = Environment.GetEnvironmentVariable("windir"); //or systemroot
Console.WriteLine("Windows OS is installed on: '{0}'", windir);
//displays: Windows OS is installed on 'C:\Windows'

//get drive info...
DriveInfo drive = DriveInfo.GetDrives()
		.Where(d=>windir.Contains(d.Name))
		.SingleOrDefault();

//further logic here....



For further details, please see:
Environment.SpecialFolder Enum (System) | Microsoft Docs[^]
Environment.GetLogicalDrives Method (System) | Microsoft Docs[^]
How to Retrieve the REAL Hard Drive Serial Number[^]
 
Share this answer
 
Comments
[no name] 16-May-19 9:27am    
nice, thanks to your solution I was able to get the serial number very quick
Maciej Los 16-May-19 9:29am    
You're very welcome.
If my answer was helpful, please accept it by using green button.
phil.o 16-May-19 10:02am    
He could not accept since he is not the OP.
The account has a pretty tough smell of spam.
Have my 5, btw :)
Maciej Los 16-May-19 15:28pm    
Thank you!
You're also making the mistake of assuming every drive has a serial number. That's not the case, especially so in "remanufactured" drives.
 
Share this answer
 
Comments
phil.o 16-May-19 10:03am    
I did not know that. Thanks for the information.
Dave Kreskowiak 16-May-19 10:20am    
It's also true that not every SSD has a serial number. You can even get entire batches of SSDs that all have the same serial number!
Maciej Los 16-May-19 15:29pm    
Agree!
Member 14052128 17-May-19 1:18am    
what if i have two hard disk which acts as master and slave the slave could be removed any time and can be changed if that happens it cause a probelm like the software is not activated(assuming while activation it tooks the serial number of slave).Thts what the problem iam facing?
Dave Kreskowiak 17-May-19 7:35am    
There hasn't been any such thing as "master" and "slave" drives since IDE, so this doesn't make sense and has no impact on what you're trying to do anyway.

I'm not telling how to fix this. I'm telling you that you're making assumptions on how stuff works that are not valid and basing your entire "licensing" scheme on it. I'm telling you to forget doing it.

What are you going to do when your code is running on a copy of Windows that's installed on a cheap-ass knockoff SSD that doesn't have a serial number? What are you going to do when two machines apparently have the same serial number? Your scheme is now screwed.

If you're looking for licensing your app, use a professional package to do it. Do not "roll your own".

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