8

At first it may seems it is very easy question and some body may be trying to give me advice to try Google, it may be so. But for me it is very hard I have try Google, Stack Overflow and can’t find any good solution.

Just want to get Serial number of Hard Disk or Hard Drive using C#

Please read carefully: serial number of Hard Disk, but not Serial number of Volume of Hard Disk (e.g. C, D, E, etc).

For getting serial no of volume of hard disk I have found solution on net and its work well but problem is with Getting serial number of Hard Disk.

Some body may trying to make this question as possible copy of below Stake Overflow question or may suggest link of that question. But it is not

And not any below question provides good solution for this problem in C#:

  1. How to get Hard-Disk SerialNumber in C# (no WMI)?
  2. How to retrieve HDD Firmware Serial number in .net?
  3. Hdd Serial Number
Community
  • 1
  • 1
Pritesh
  • 3,208
  • 9
  • 51
  • 70
  • 1
    I don't understand. You already provided a link to the answer. It's the article at the very bottom with the screenshot you liked so much. What's "boring" about C++? – Cody Gray - on strike Apr 15 '11 at 11:32
  • And more importantly, why do so many people need to find out the serial number of my hard drive? This question gets asked a lot (you found at least 3 duplicates already), considering there's *absolutely no valid use case*. The volume serial number is the only thing you could *possibly* care about; it's the only thing that matters. – Cody Gray - on strike Apr 15 '11 at 11:33
  • because i trying but could not implement it in C#..... and i using Serial no of Hard Disk for our application which only run in client PC if it found hard disk serial no of client PC in the Database of Executable of our application.....In SHORT it matches hard Disk Serial number one in Database and One getting at run time from client PC.........if found than and than our application will run..... – Pritesh Apr 15 '11 at 11:42
  • 1
    @CodyGray I'm sure there's a use case considering this question has 6k views. I'm working on a project where we are replacing old hard drives and need the hard drive serials. I'm looking for a way to pull this information remotely. – JSuar Aug 10 '12 at 09:31

6 Answers6

9

This is the final solution:

Get Physical HDD Serial Number without WMI

write this much code:

DriveListEx diskInfo = new DriveListEx();
diskInfo.Load();
string serialNo = diskInfo[0].SerialNumber;

Don't forgot to add reference to the DriveInfoEx.dll.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Pritesh
  • 3,208
  • 9
  • 51
  • 70
  • 1
    Why not simply *translate* the unmanaged C++ code from the DLL into managed C#? It's not doing anything that's impossible from a managed language. Then you eliminate the dependency on a third-party DLL. – Cody Gray - on strike Apr 20 '11 at 03:47
  • @ Cody Gray, but for me it is harder.......or i even don't try yet......... if you can then post it here i will accept your answer........... Thanks....... – Pritesh Apr 20 '11 at 04:19
  • 1
    Good Solution: But some times it is not working...i tried it on 4 pcs, it didn't work on 1st pc but worked when right clicked the EXE and selected "run as admin" option..worked fine for 2nd and 3rd pc...on 4th pc which was not having admin rights,it didnt work as exception occurred.. – Sangram Nandkhile Aug 12 '11 at 08:03
  • 2
    DriveInfoex.dll doesn't work windows 7 OS. it's throw the below Error System.IO.FileLoadException: Could not load file or assembly 'DriveInfoEx.dll' or one of its dependencies. This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1) –  Nov 07 '11 at 07:03
  • @EvgeniyKrechun, means what please elaborate? – Pritesh Nov 11 '11 at 05:20
4

see this

http://www.codeproject.com/KB/system/GetHardwareInformation.aspx

just download demo from there and select "data storage" tab and select Win32_DiskDrive from this you will get information all the Disk drives(HardDisk) mention below and see one property "SerialNumber" after sectorpertrack and before signature property...

enter image description here

asharajay
  • 1,184
  • 9
  • 19
  • 1
    you can see screen shot over belove link what happen when i running the application........you will not find "SerialNumber" field.... LINK:http://pritesharyan.weebly.com/question3.html – Pritesh Apr 15 '11 at 11:36
  • 1
    It could be the system. I have googled a bit, and a lot of the solutions are rapported to not work on Windows XP and 64bit windows systems. You may need to find multiple solutions depending on the system the software should run on – CasperT Apr 15 '11 at 11:49
  • no i dnt think so its problem of WinXp. bz i used it in WinXP for the Application registration purpose i want hardisk serial number,motherboard and processor... for making Product Key. at tat time it was working fine.. mean i m getting everythng ... i think its prob of harddrive/motherboard/IDE controller... but i m not right person to answer that wat is problem in h/w... sory for tat dear.. – asharajay Apr 15 '11 at 11:54
  • @jAX,it's ok dear ........ i also using for the same purpose.....but i am stuck up thanks for replying............... – Pritesh Apr 15 '11 at 11:56
2

The best way I found is:

  1. Download the .dll from here

  2. Add the .dll to your project

  3. Add this code:

    [DllImportAttribute("HardwareIDExtractorC.dll")]
    public static extern String GetIDESerialNumber(byte DriveNumber);

  4. Call the hard disk ID from where you need it:

    GetIDESerialNumber(0).Replace(" ", string.Empty);

Note: Go to the properties of the dll in explorer and set Build Action to Embedded Resource.

Scott
  • 21,211
  • 8
  • 65
  • 72
1
// Function driveser (model)
// Returns the serial number of the drive specified in "model" or an empty string. 
// Please include this is you are going to use it.
// (C) By Zibri 2013
// Free for non commercial use.
// zibri AT zibri DOT org

public string driveser(string model)
{
    string functionReturnValue = null;
    string devid = "";
    functionReturnValue = "";
    try {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive WHERE Model LIKE '%" + model + "%'");
        foreach (ManagementObject queryObj in searcher.Get()) {
            if (!string.IsNullOrEmpty(queryObj("SerialNumber")))
                functionReturnValue = queryObj("SerialNumber");
            Debug.Print(queryObj("Model") + ":" + functionReturnValue);
        }
    } catch (ManagementException err) {
        Debug.Print("An error occurred while querying for WMI data: " + err.Message);
    }
    return functionReturnValue;
}
Zibri
  • 9,096
  • 3
  • 52
  • 44
1

I took a look with ILSpy (http://ilspy.net/) to System.IO.DriveInfo class and I figured out this code that seems to work fine :

'------------------------------------------------------
' Declaration found in Microsoft.Win32.Win32Native
'------------------------------------------------------
Friend Declare Auto Function GetVolumeInformation Lib "kernel32.dll" (drive As String, <Out()> volumeName As StringBuilder, volumeNameBufLen As Integer, <Out()> ByRef volSerialNumber As Integer, <Out()> ByRef maxFileNameLen As Integer, <Out()> ByRef fileSystemFlags As Integer, <Out()> fileSystemName As StringBuilder, fileSystemNameBufLen As Integer) As Boolean

'------------------------------------------------------
' Test in my Form class
'------------------------------------------------------
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    Try
        Dim volumeName As StringBuilder = New StringBuilder(50)
        Dim stringBuilder As StringBuilder = New StringBuilder(50)
        Dim volSerialNumber As Integer
        Dim maxFileNameLen As Integer
        Dim fileSystemFlags As Integer
        If Not GetVolumeInformation("C:\", volumeName, 50, volSerialNumber, maxFileNameLen, fileSystemFlags, stringBuilder, 50) Then
            Dim lastWin32Error As Integer = Marshal.GetLastWin32Error()
            MsgBox("Error number:" & lastWin32Error)
        Else
            MsgBox(volSerialNumber.ToString("X"))
        End If

    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try
End Sub
Massimo
  • 137
  • 1
  • 4
0

I found a very good library that do what you want : https://www.nuget.org/packages/Hardware.Info/10.0.1?_src=template

using Hardware.Info;

IHardwareInfo hardwareInfo = new HardwareInfo();

hardwareInfo.RefreshDriveList();

foreach (var drive in hardwareInfo.DriveList)
{
    Console.WriteLine(drive.SerialNumber);
}
ihebiheb
  • 3,673
  • 3
  • 46
  • 55