Serial number of Hard Disk or Hard Drive

前端 未结 5 2009
滥情空心
滥情空心 2020-12-14 12:30

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

相关标签:
5条回答
  • 2020-12-14 13:10

    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

    0 讨论(0)
  • 2020-12-14 13:10

    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.

    0 讨论(0)
  • 2020-12-14 13:14

    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.

    0 讨论(0)
  • 2020-12-14 13:17

    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
    
    0 讨论(0)
  • 2020-12-14 13:22
    // 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;
    }
    
    0 讨论(0)
提交回复
热议问题