Read the hard disk serial number

Hi.

I need to read the serial number of the hard disk
LVL 18
Elvio Lujan.Net Senior DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Level10AccessCommented:
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
iboutchkineCommented:
Here are another 2 examples

Imports System.Management

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mc As New ManagementClass("Win32_BIOS")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        Dim enu As ManagementObjectCollection.ManagementObjectEnumerator = moc.GetEnumerator
        enu.MoveNext()
        MsgBox("Serial No: " & enu.Current.Properties("SerialNumber").Value.ToString)

    End Sub
End Class

===============================================


Adding a reference to Microsoft Scripting Runtime COM component


Imports Scripting

Dim fso As New FileSystemObject()
MessageBox.Show(fso.GetDrive("C").SerialNumber.ToString)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gates_VBNetCommented:
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
Gates_VBNetCommented:
I forgot it . Add this line to top of the Public Class FormName :

Imports System.Management
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.