Serial number of Hard Disk or Hard Drive

拈花ヽ惹草 提交于 2019-11-27 13:17:25

问题


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

回答1:


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.




回答2:


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...




回答3:


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.




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/5675761/serial-number-of-hard-disk-or-hard-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!