Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Article

How to Retrieve the REAL Hard Drive Serial Number

Rate me:
Please Sign up or sign in to vote.
4.64/5 (120 votes)
17 Feb 2004CPOL3 min read 1.2M   53.8K   251   186
Shows you how to obtain the hardware serial number set by the manufacturer and not the Volume Serial Number that changes after you format a hard drive.
Sample Image - hard_disk_serialNo.gif

Introduction

Making sure your software is used by legal buyers is a concern for programmers around the world. My professor once said that we shouldn’t give 100% of our code to the users because there are people out there that are smart enough to decompile our programs and find the various verification algorithms used. He suggested that we give users 99% of our software, but keep the remaining 1% to ourselves. This 1% is the verification algorithm to confirm only valid users can use the program; this is commonly known as “activation.”

Activation is good, but it means our software users will need to have Internet access and that means small programmers like us have to set up a server that can validate users. Of course, only big companies with big clients can afford to do this. For the rest of us, we have to think of other ways.

One method programmers have used since the DOS era was to bind their software to the Hard Drive Volume Serial Number. This is not a good choice, as later we all find out that every time we format the same hard drive, a new Volume Serial Number is generated.

Background

A better solution is to get the Hard Drive Serial Number given by the Manufacturer. This value won't change even if you format your Hard Drive. Of course, if people buy new hard drive, we have a problem. But at least we keep the regular hard-drive formatters happy!

The Code

First, let's create a class to store information about a hard drive:

C#
class HardDrive
{
 private string model = null;
 private string type = null;
 private string serialNo = null; 
 public string Model
 {
  get {return model;}
  set {model = value;}
 } 
 public string Type
 {
  get {return type;}
  set {type = value;}
 } 
 public string SerialNo
 {
  get {return serialNo;}
  set {serialNo = value;}
 }
}

Next, add a reference to your project. Scroll down to System.Management under ComponentName. This reference is not provided by default, so you need to add it.

Add a "using System.Management;" at the top of your source code. System.Management allows us to access WMI objects. Put simply, WMI contains a lot information about your hardware.

Now there's a problem: the hard drive model is found in the Win32_DiskDrive class and the serial number is found in the Win32_PhysicalMedia class. Thus, we need to query twice and integrate the information into our HardDrive class.

Let's first create an ArrayList to store our HardDrive objects: ArrayList hdCollection = new ArrayList();. Next, we query the Win32_DiskDrive class:

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

foreach(ManagementObject wmi_HD in searcher.Get())
{
 HardDrive hd = new HardDrive();
 hd.Model = wmi_HD["Model"].ToString();
 hd.Type  = wmi_HD["InterfaceType"].ToString();
 hdCollection.Add(hd);
}

Now we need to extract the serial number from the Win32_PhysicalMedia class:

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

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
 // get the hard drive from collection
 // using index
 HardDrive hd = (HardDrive)hdCollection[i];

 // get the hardware serial no.
 if (wmi_HD["SerialNumber"] == null)
  hd.SerialNo = "None";
 else
  hd.SerialNo = wmi_HD["SerialNumber"].ToString();

 ++i;
}

Luckily, the WMI objects from Win32_DiskDrive are in the same order as from Win32_PhysicalMedia, so the simple code above works. Otherwise, we will have to match them using their unique DeviceID. Start a thread in this article if you think Win32_DiskDrive objects are not the same order as Win32_PhysicalMedia objects returned.

We are done! Now we display our hard drive's information:

C#
// Display available hard drives
foreach(HardDrive hd in hdCollection)
{
 Console.WriteLine("Model\t\t: " + hd.Model);
 Console.WriteLine("Type\t\t: " + hd.Type);
 Console.WriteLine("Serial No.\t: " + hd.SerialNo);
 Console.WriteLine();
}

Conclusion

If a user has more than one hard drive, I suggest you display the available hard drives to the user. Ask him which hard drive he's not planning to upgrade soon, then use that hard drive serial number to generate a license key. This will prevent your user from nagging for a new license key in short time.

If your user finally decides to upgrade hard drive, you may give this article's demo application to the user and ask him what the serial number is. Alternatively, you can create a much more user-friendly GUI one. :)

Hope you find this useful.

License

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


Written By
Web Developer
Indonesia Indonesia
Zeddy Iskandar, computer science student. Writes freewares on free time. Regularly rambles on his blog. Likes to experiment on emerging software technologies.

www.zedilabs.com

Comments and Discussions

 
GeneralSerial No. : None Pin
Martin Vermehren24-Feb-09 19:25
Martin Vermehren24-Feb-09 19:25 
GeneralRe: Serial No. : None Pin
ibhatti819-Nov-09 8:00
ibhatti819-Nov-09 8:00 
GeneralSerial number for USB drive. Pin
Shashi.Shinde23-Nov-08 19:51
Shashi.Shinde23-Nov-08 19:51 
GeneralRe: Serial number for USB drive. Pin
jdadhaniya10-Feb-09 4:56
jdadhaniya10-Feb-09 4:56 
Questiondoes anybody know how to do it without windows ? Pin
AbstractCure4-Nov-08 10:12
AbstractCure4-Nov-08 10:12 
QuestionDoes it work when the SMART... [modified] Pin
Hasan Gürsoy17-Jul-08 11:01
Hasan Gürsoy17-Jul-08 11:01 
QuestionDon't Work in Windows Vista Pin
Armadar23-May-08 13:17
Armadar23-May-08 13:17 
AnswerRe: Don't Work in Windows Vista Pin
jrwiskerke25-May-08 23:54
jrwiskerke25-May-08 23:54 
I found this link about WMI and the UAC. You might want to try if it provides the solution.

http://msdn.microsoft.com/en-us/library/aa826699.aspx

Anyone who has Vista, please respond whether it solves your problem.
GeneralRe: Don't Work in Windows Vista Pin
Derek Bartram28-May-08 0:36
Derek Bartram28-May-08 0:36 
AnswerRe: Don't Work in Windows Vista Pin
avenger-france29-Dec-08 6:01
avenger-france29-Dec-08 6:01 
QuestionWhy Serial number is null Pin
Ehsan Golkar7-Mar-08 19:29
Ehsan Golkar7-Mar-08 19:29 
GeneralRe: Why Serial number is null Pin
novadon6-Apr-08 4:35
novadon6-Apr-08 4:35 
GeneralRe: Why Serial number is null Pin
z1616616-Sep-08 23:37
z1616616-Sep-08 23:37 
GeneralRe: Why Serial number is null Pin
acbll19-Dec-09 6:54
acbll19-Dec-09 6:54 
AnswerRe: Why Serial number is null Pin
mano_meee18-Jul-09 23:43
mano_meee18-Jul-09 23:43 
GeneralRe: Why Serial number is null Pin
NoviceVK10-May-10 18:13
NoviceVK10-May-10 18:13 
General"Invalid Class" exception when calling searcher.Get for Win32_PhysicalMedia Pin
plevintampabay29-Feb-08 11:49
plevintampabay29-Feb-08 11:49 
GeneralRe: "Invalid Class" exception when calling searcher.Get for Win32_PhysicalMedia Pin
z1616616-Sep-08 23:39
z1616616-Sep-08 23:39 
GeneralCould you teach me how to implement with other language Pin
Kelly Cristina Mara11-Feb-08 10:26
Kelly Cristina Mara11-Feb-08 10:26 
GeneralThx toooooooooooooooo much Pin
Ahmad_Ramadan12-Dec-07 1:21
Ahmad_Ramadan12-Dec-07 1:21 
GeneralRe: Thx toooooooooooooooo much Pin
novadon15-Jan-08 23:40
novadon15-Jan-08 23:40 
GeneralHard Disk SerialNumber Pin
Member 44519864-Dec-07 17:57
Member 44519864-Dec-07 17:57 
GeneralRe: Hard Disk SerialNumber Pin
z1616616-Sep-08 23:42
z1616616-Sep-08 23:42 
Questioncan get HDD serial key in Vista? Pin
Stenal1-Dec-07 22:30
Stenal1-Dec-07 22:30 
GeneralHi sorry but ur demo application shows a error Pin
zLord31-Oct-07 11:01
zLord31-Oct-07 11:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.