硬盘或硬盘的序列号

时间:2011-04-15 11:06:19

标签: c# .net hard-drive serial-number

起初看起来这可能是一个非常简单的问题,有些机构可能会试图给我建议尝试谷歌,可能是这样。 但对我来说,我很难尝试Google,Stack Overflow并且找不到任何好的解决方案。

只想使用C#

获取硬盘或硬盘的序列号

请仔细阅读:硬盘序列号,但不是硬盘卷序列号(例如C,D,E等)。

为了获得硬盘卷的序列号,我在网上找到了解决方案并且工作正常,但问题在于获取硬盘的序列号。

某些机构可能会尝试将此问题作为下面的“放样溢出”问题的副本,或者可能会建议该问题的链接。但它不是

以下任何问题都没有为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

5 个答案:

答案 0 :(得分:8)

这是最终解决方案:

Get Physical HDD Serial Number without WMI

写下这么多代码:

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

不要忘记添加对DriveInfoEx.dll的引用。

答案 1 :(得分:4)

看到这个

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

从那里下载演示并选择“数据存储”选项卡并从中选择Win32_DiskDrive,您将获得下面提到的所有磁盘驱动器(HardDisk)的信息,并在sectorpertrack之后和签名属性之前看到一个属性“SerialNumber”...... / p>

enter image description here

答案 2 :(得分:2)

我发现的最好方法是:

  1. here

  2. 下载.dll
  3. 将.dll添加到您的项目

  4. 添加此代码:

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

  5. 从您需要的位置调用硬盘ID:

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

  6. 注意:转到资源管理器中的dll属性,并将Build Action设置为Embedded Resource

答案 3 :(得分: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;
}

答案 4 :(得分:1)

我看了一下ILSpy(http://ilspy.net/)到System.IO.DriveInfo类,我发现了 这段代码似乎工作正常:

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