Click here to Skip to main content
11,412,833 members (73,353 online)
Click here to Skip to main content

How to Retrieve the REAL Hard Drive Serial Number

, 17 Feb 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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


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.


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:

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:

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

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

   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";
     hd.SerialNo = wmi_HD["SerialNumber"].ToString();

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:

   // 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);


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. Smile | :)

Hope you find this useful.


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


About the Author

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.

Comments and Discussions

QuestionHow about Read HDD Serial Number under NT Authority\Network Service Pin
PhiHung at 22-Mar-15 19:54
memberPhiHung22-Mar-15 19:54 
QuestionWhy not get all the data Pin
theMadCoder at 20-Nov-13 11:21
membertheMadCoder20-Nov-13 11:21 
QuestionC++ Pin
Bogdan Calita at 19-Nov-13 2:25
memberBogdan Calita19-Nov-13 2:25 
AnswerRe: C++ Pin
kezhu at 18-May-14 18:15
memberkezhu18-May-14 18:15 
GeneralMy vote of 5 Pin
jgodino at 20-Sep-13 10:51
memberjgodino20-Sep-13 10:51 
GeneralMy vote of 5 Pin
hims056 at 31-Oct-12 20:35
memberhims05631-Oct-12 20:35 
Questionsimple solution Pin
azhar.bandri at 29-Sep-12 2:49
memberazhar.bandri29-Sep-12 2:49 
GeneralMy vote of 4 Pin
snorkie at 12-Sep-12 5:00
membersnorkie12-Sep-12 5:00 
BugBUG Pin
sehathesam at 7-Aug-12 23:32
membersehathesam7-Aug-12 23:32 
Questionwhy it's not the same Pin
beyondlwm at 16-Jun-12 1:24
memberbeyondlwm16-Jun-12 1:24 
AnswerRe: why it's not the same Pin
PhiHung at 22-Mar-15 19:56
memberPhiHung22-Mar-15 19:56 
QuestionHard Drive Serial Number in UBUNTU Pin
J. manuel vargas at 14-May-12 8:29
memberJ. manuel vargas14-May-12 8:29 
GeneralGood Pin
y_sir at 10-May-12 2:29
membery_sir10-May-12 2:29 
Question[My vote of 2] Nice try, but needs more work. Pin
Jan Zumwalt at 28-Feb-12 9:01
memberJan Zumwalt28-Feb-12 9:01 
QuestionThe code above is unaccurate and misleading Pin
Member 692536 at 1-Oct-11 18:36
memberMember 6925361-Oct-11 18:36 
GeneralAdministrator and ordinary user give different result. Pin
orionkubilay at 9-Jun-11 23:23
memberorionkubilay9-Jun-11 23:23 
GeneralRe: Administrator and ordinary user give different result. Pin
Bronek at 17-Jul-14 3:04
memberBronek17-Jul-14 3:04 
GeneralSATA Pin
Issa.Alkaht at 5-Jun-11 6:03
memberIssa.Alkaht5-Jun-11 6:03 
GeneralMy vote of 2 Pin
hafizhanif at 1-Feb-11 0:36
memberhafizhanif1-Feb-11 0:36 
GeneralVMs problem Pin
mla3beh at 17-Jan-11 8:00
membermla3beh17-Jan-11 8:00 
GeneralMy vote of 2 Pin
Member 1643792 at 18-Dec-10 6:01
memberMember 164379218-Dec-10 6:01 
GeneralMy vote of 5 Pin
Member 4179762 at 2-Nov-10 3:39
memberMember 41797622-Nov-10 3:39 
QuestionWhy is it returning null?? Pin
nethsu at 24-Oct-10 23:55
membernethsu24-Oct-10 23:55 
GeneralJunk code Pin
Totte Karlsson at 20-Oct-10 14:42
memberTotte Karlsson20-Oct-10 14:42 
GeneralNot Work with Framework 4.0 Pin
kalpesh2804 at 28-Sep-10 23:42
memberkalpesh280428-Sep-10 23:42 
GeneralIndex was out of range Pin
SamanُSami at 9-Jul-10 3:21
memberSamanُSami9-Jul-10 3:21 
GeneralRe: Index was out of range Pin
xpectworld at 11-Nov-12 6:37
memberxpectworld11-Nov-12 6:37 
GeneralMy vote of 3 Pin
vikaskardode at 8-Jul-10 23:23
membervikaskardode8-Jul-10 23:23 
GeneralWin32_PhysicalMedia empty retrieving from a webservice Pin
javitgal at 22-Jun-10 7:18
memberjavitgal22-Jun-10 7:18 
GeneralA mistake Pin
namarie at 9-Jun-10 3:14
membernamarie9-Jun-10 3:14 
SuggestionRe: A mistake Pin
DLChambers at 15-Jul-11 11:52
memberDLChambers15-Jul-11 11:52 
GeneralMy vote of 1 Pin
forcedregsux at 1-Jun-10 1:47
memberforcedregsux1-Jun-10 1:47 
GeneralSerial No as None Please Help Pin
Member 1660230 at 30-Sep-09 21:33
memberMember 166023030-Sep-09 21:33 
GeneralRe: Serial No as None Please Help Pin
dmihailescu at 18-Nov-09 14:16
memberdmihailescu18-Nov-09 14:16 
Generali have a question -wrong in wndProc Pin
Member 4664058 at 19-Sep-09 7:54
memberMember 466405819-Sep-09 7:54 
Generalhard driver serial number Pin
Wendel Renner at 18-Jun-09 3:56
memberWendel Renner18-Jun-09 3:56 
GeneralRe: hard driver serial number Pin
Michael Patterson II at 25-Aug-11 15:14
memberMichael Patterson II25-Aug-11 15:14 
GeneralMy vote of 1 Pin
Member 3964159 at 12-Apr-09 13:09
memberMember 396415912-Apr-09 13:09 
GeneralMy vote of 2 Pin
Patricker at 27-Mar-09 17:34
memberPatricker27-Mar-09 17:34 
GeneralLink Police Pin
Jerry Hammond at 11-Apr-09 13:51
memberJerry Hammond11-Apr-09 13:51 
GeneralWhy it does not work, and how you can sort of fix it. Pin
Patricker at 27-Mar-09 17:32
memberPatricker27-Mar-09 17:32 
GeneralRe: Why it does not work, and how you can sort of fix it. Pin
DLChambers at 15-Jul-11 11:55
memberDLChambers15-Jul-11 11:55 
GeneralWMI query doesnot return record for user having less previleges!! Pin
deep@Pune at 2-Mar-09 21:02
memberdeep@Pune2-Mar-09 21:02 
GeneralSerial No. : None Pin
Martin Vermehren at 24-Feb-09 20:25
memberMartin Vermehren24-Feb-09 20:25 
GeneralRe: Serial No. : None Pin
ibhatti81 at 9-Nov-09 9:00
memberibhatti819-Nov-09 9:00 
GeneralSerial number for USB drive. Pin
Shashi.Shinde at 23-Nov-08 20:51
memberShashi.Shinde23-Nov-08 20:51 
GeneralRe: Serial number for USB drive. Pin
jdadhaniya at 10-Feb-09 5:56
memberjdadhaniya10-Feb-09 5:56 
Questiondoes anybody know how to do it without windows ? Pin
AbstractCure at 4-Nov-08 11:12
memberAbstractCure4-Nov-08 11:12 
QuestionDoes it work when the SMART... [modified] Pin
x-freestyler at 17-Jul-08 12:01
memberx-freestyler17-Jul-08 12:01 
QuestionDon't Work in Windows Vista Pin
Armadar at 23-May-08 14:17
memberArmadar23-May-08 14:17 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 18 Feb 2004
Article Copyright 2004 by zeddy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid