Vb.net – How to read HDD volume serial number using VB.NET

hard drivevb.net

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.

Best Answer

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