Link to home
Start Free TrialLog in
Avatar of Elvio Lujan
Elvio LujanFlag for Argentina

asked on

Read the hard disk serial number

Hi.

I need to read the serial number of the hard disk
Avatar of sajuks
sajuks

Private Declare Function GetVolumeInformation _
      Lib "kernel32" Alias "GetVolumeInformationA" _
      (ByVal lpRootPathName As String, _
      ByVal pVolumeNameBuffer As String, _
      ByVal nVolumeNameSize As Long, _
      lpVolumeSerialNumber As Long, _
      lpMaximumComponentLength As Long, _
      lpFileSystemFlags As Long, _
      ByVal lpFileSystemNameBuffer As String, _
      ByVal nFileSystemNameSize As Long) As Long

Public Function GetSerialNumber( _
      ByVal sDrive As String) As Long

      If Len(sDrive) Then
            If InStr(sDrive, "\\") = 1 Then
                  ' Make sure we end in backslash for UNC
                  If Right$(sDrive, 1) <> "\" Then
                        sDrive = sDrive & "\"
                  End If
            Else
                  ' If not UNC, take first letter as drive
                  sDrive = Left$(sDrive, 1) & ":\"
            End If
      Else
            ' Else just use current drive
            sDrive = vbNullString
      End If
      
      ' Grab S/N -- Most params can be NULL
      Call GetVolumeInformation( _
            sDrive, vbNullString, 0, GetSerialNumber, _
            ByVal 0&, ByVal 0&, vbNullString, 0)
End Function

'-- Form code
Private Sub Command1_Click()
      Dim Drive As String
      Drive = InputBox("Enter drive for checking SN")
      MsgBox Hex$(GetSerialNumber(Drive))
End Sub
ASKER CERTIFIED SOLUTION
Avatar of iboutchkine
iboutchkine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello .
You can use this one .

1 -  Add System.Management refrence to your project .
2 - Add a Button Control named "Button1" to your project .

'Use the following code...

  Class HardDrive
        Private model As String = Nothing
        Private type As String = Nothing
        Private serialNo As String = Nothing

        Public Property gModel() As String
            Get
                Return Model
            End Get
            Set(ByVal Value As String)
                model = Value
            End Set
        End Property
        Public Property gType() As String
            Get
                Return type
            End Get
            Set(ByVal Value As String)
                type = Value
            End Set
        End Property
        Public Property gSerialNo() As String
            Get
                Return serialNo
            End Get
            Set(ByVal Value As String)
                serialNo = Value
            End Set
        End Property
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Result As String
        Dim hdCollection As ArrayList = New ArrayList
        Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
        For Each wmi_HD As ManagementObject In searcher.Get
            Dim hd As HardDrive = New HardDrive
            hd.gModel = wmi_HD("Model").ToString
            hd.gType = wmi_HD("InterfaceType").ToString
            hdCollection.Add(hd)
        Next
        searcher = New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
        Dim i As Integer = 0
        For Each wmi_HD As ManagementObject In searcher.Get
            Dim hd As HardDrive = CType(hdCollection(i), HardDrive)
            If wmi_HD("SerialNumber") Is Nothing Then
                hd.gSerialNo = "None"
            Else
                hd.gSerialNo = wmi_HD("SerialNumber").ToString
            End If
            System.Threading.Interlocked.Increment(i)
        Next
        For Each hd As HardDrive In hdCollection
            Result = "Model : " & hd.gModel & vbCrLf
            Result &= "Type : " & hd.gType & vbCrLf
            Result &= "Serial No : " & hd.gSerialNo
        Next
        MessageBox.Show(Result)
    End Sub
I forgot it . Add this line to top of the Public Class FormName :

Imports System.Management