How can I read HDD volume serial number using VB.N

2019-08-09 01:30发布

问题:

How can I read HDD volume serial number using VB.NET but without any third party addons for Visual Studio or any external libraries - I neeed native VB.NET code for this if possible.

回答1:

Not sure if this is what you're looking for but a quick google search lead me to this website:

http://www.vbgold.com/vb-projects/disk-serial-number.shtml



回答2:

Public Function GetDriveSerialNumber() As String
    Dim DriveSerial As Long
    Dim fso As Object, Drv As Object
    'Create a FileSystemObject object
    fso = CreateObject("Scripting.FileSystemObject")
    Drv = fso.GetDrive(fso.GetDriveName(AppDomain.CurrentDomain.BaseDirectory))
    With Drv
        If .IsReady Then
            DriveSerial = .SerialNumber
        Else    '"Drive Not Ready!"
            DriveSerial = -1
        End If
    End With
    'Clean up
    Drv = Nothing
    fso = Nothing
    GetDriveSerialNumber = Hex(DriveSerial)
End Function


回答3:

' 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

Function driveser(ByVal model As String) As String
    Dim devid As String = ""
    driveser = ""
    Try
        Dim searcher As New ManagementObjectSearcher( _
            "root\CIMV2", _
            "SELECT * FROM Win32_DiskDrive WHERE Model LIKE '%" + model + "%'")
        For Each queryObj As ManagementObject In searcher.Get()
            If queryObj("SerialNumber") <> "" Then driveser = queryObj("SerialNumber")
            Debug.Print(queryObj("Model") + ":" + driveser)
        Next
    Catch err As ManagementException
        Debug.Print("An error occurred while querying for WMI data: " & err.Message)
    End Try
End Function