硬盘或硬盘驱动器的序列号

发布于 2024-11-01 17:38:17 字数 718 浏览 5 评论 0原文

乍一看,这似乎是一个非常简单的问题,有些人可能试图给我尝试谷歌的建议,可能确实如此。 但对我来说这很难,我尝试了 Google、Stack Overflow,但找不到任何好的解决方案。

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

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

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

有些机构可能会尝试将此问题作为以下 Stake Overflow 问题的可能副本,或者可能会建议该问题的链接。但事实并非如此,

并且以下任何问题都没有为 C# 中的此问题提供良好的解决方案:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

熟人话多 2024-11-08 17:38:17

这是最终的解决方案:

Get Physical HDD Serial Number without WMI

写这么多代码:

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

别忘了添加对 DriveInfoEx.dll 的引用。

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.

夜深人未静 2024-11-08 17:38:17

看到这个

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

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

在此处输入图像描述

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

归途 2024-11-08 17:38:17

我发现的最好方法是:

  1. 此处

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

  3. 添加以下代码:

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

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

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

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

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.

定格我的天空 2024-11-08 17:38:17
// 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;
}
// 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;
}
二智少女猫性小仙女 2024-11-08 17:38:17

我用 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

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
夜夜流光相皎洁 2024-11-08 17:38:17

我找到了一个非常好的库,可以满足您的要求: 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文