8

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

只想使用 C# 获取硬盘或硬盘的序列号

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

为了获得硬盘卷的序列号,我在网上找到了解决方案,它工作得很好,但问题是获取硬盘的序列号。

某些机构可能会尝试将此问题作为以下 Stake Overflow 问题的可能副本,或者可能会建议该问题的链接。但它不是

并且没有任何以下问题为 C# 中的这个问题提供了很好的解决方案:

  1. 如何在 C#(无 WMI)中获取硬盘序列号?
  2. 如何在.net 中检索硬盘固件序列号?
  3. 硬盘序列号
4

5 回答 5

9

这是最终的解决方案:

在没有 WMI 的情况下获取物理硬盘序列号

写这么多代码:

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

不要忘记添加对DriveInfoEx.dll.

于 2011-04-18T05:28:21.657 回答
4

看到这个

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

只需从那里下载演示并选择“数据存储”选项卡并从中选择 Win32_DiskDrive,您将获得下面提到的所有磁盘驱动器(硬盘)的信息,并在扇区跟踪之后和签名属性之前看到一个属性“序列号”...

在此处输入图像描述

于 2011-04-15T11:09:02.753 回答
2

我发现的最好方法是:

  1. 从这里下载 .dll

  2. 将 .dll 添加到您的项目中

  3. 添加此代码:

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

  4. 从您需要的地方调用硬盘 ID:

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

注意:在资源管理器中转到 dll 的属性并设置Build ActionEmbedded Resource.

于 2012-09-29T11:51:19.983 回答
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;
}
于 2013-03-01T02:16:52.013 回答
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
于 2013-09-18T14:32:48.850 回答