Serial number of Hard Disk or Hard Drive
Asked Answered
C

6

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
Cosmetic answered 15/4, 2011 at 11:6 Comment(4)
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++?Megrims
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.Megrims
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.....Cosmetic
@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.Qktp
C
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.

Cosmetic answered 18/4, 2011 at 5:28 Comment(5)
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.Megrims
@ 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.......Cosmetic
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..Maneating
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)Ferdinande
@EvgeniyKrechun, means what please elaborate?Cosmetic
C
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

Casserole answered 15/4, 2011 at 11:9 Comment(4)
you can see screen shot over belove link what happen when i running the application........you will not find "SerialNumber" field.... LINK:pritesharyan.weebly.com/question3.htmlCosmetic
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 onRupertruperta
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..Casserole
@jAX,it's ok dear ........ i also using for the same purpose.....but i am stuck up thanks for replying...............Cosmetic
K
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.

Kazimir answered 29/9, 2012 at 11:51 Comment(0)
S
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;
}
Sublapsarianism answered 1/3, 2013 at 2:16 Comment(0)
M
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
Miscreant answered 18/9, 2013 at 14:32 Comment(0)
C
1

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);
}
Carmella answered 6/9, 2022 at 22:35 Comment(1)
Super, and its MIT licensed.Revanchism

© 2022 - 2025 — McMap. All rights reserved.