Get H/D Serial number (Not Volumn Serial Number) for IDE and SATA

谁说胖子不能爱 提交于 2019-12-18 07:04:51

问题


How can I read the hard disk serial number for IDE and SATA drives in VB.NET? (I don't want the volume serial number).

This info should be gathered both for XP and Vista if possible without administrative rights.


回答1:


You can use WMI (Windows Management Instrumentation) like this:

Dim mos As New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")

For Each mo As ManagementObject In mos.Get()
    Dim serial As String = mo("SerialNumber").ToString()
Next

Although, I've read about cases in which no serial number is returned using WMI. Another way to accomplish this would be through Platform Invocation Services (PInvoke).

This article includes a download in which the author implements CreateFile() and DeviceIoControl() to extract drive information through Interop services in VB .NET.

To use either of the above outlined methods you will need ADMIN rights, a utility which seems to circumvent this can be found here. If your feeling adventurous the C++/Win32 source code is available for you to peruse. (Check out the function 'ReadPhysicalDriveInNTWithZeroRights()')




回答2:


Public Function getHardDiskSerialNo()
    Dim serial As String

    Dim hd As New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
    For Each dvs As ManagementObject In hd.Get()
        serial = dvs("SerialNumber").ToString()

    Next
    Return serial

End Function



回答3:


Here is the code to get HDD Serial Number

    Dim HDD_Serial As String

    Dim hdd As New ManagementObjectSearcher("select * from Win32_DiskDrive")

    For Each hd In hdd.Get

        HDD_Serial = hd("SerialNumber")
        MsgBox(HDD_Serial)
    Next

Hope it Helps.



来源:https://stackoverflow.com/questions/535902/get-h-d-serial-number-not-volumn-serial-number-for-ide-and-sata

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