Please tell me How can i get HDD serial number in VB

this code show c drive serial number

Private Sub Form_Load()
      
          'Show drive serial number for the current drive
          MsgBox " Drive serial number for " & Left(App.Path, 1) & ": " & GetDriveSerialNumber
          End
      
      End Sub
      Public Function GetDriveSerialNumber(Optional ByVal DriveLetter As String) As Long
      
          Dim fso As Object, Drv As Object
          
          'Create a FileSystemObject object
          Set fso = CreateObject("Scripting.FileSystemObject")
          
          'Assign the current drive letter if not specified
          If DriveLetter <> "" Then
              Set Drv = fso.GetDrive(DriveLetter)
          Else
              Set Drv = fso.GetDrive(fso.GetDriveName(App.Path))
          End If
      
          With Drv
              If .IsReady Then
                  DriveSerial = Abs(.SerialNumber)
              Else    '"Drive Not Ready!"
                  DriveSerial = -1
              End If
          End With
          
          'Clean up
          Set Drv = Nothing
          Set fso = Nothing
          
          GetDriveSerialNumber = DriveSerial
          
      End Function

'''try to run this code

Const CheckDrive_NotRem = 0
Const CheckDrive_RemAndFound = 1
Const CheckDrive_RemAndNotFound = 2
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long


'InPut: the letter of the drive to check it or the string "all" to check for all drives
Public Function CheckNow(strDrive As String)
        
    Dim strSave As String
    Dim strDriveName As String
    Dim Res, keer, DiskFound
    
    'Create a buffer to store all the drives
    strSave = String(255, Chr$(0))
    'Get all the drives
    ret& = GetLogicalDriveStrings(255, strSave)
    
    'Extract the drives from the buffer
    For keer = 1 To 100
        If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
        strDriveName = Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
        'Check the drive for type and diskettes
        If strDrive = "all" Then
            Res = CheckDrive(strDriveName)
            If Res = CheckDrive_RemAndNotFound Then
                MsgBox "No Disk in drive: " & strDriveName
            ElseIf Res = CheckDrive_RemAndFound Then
                MsgBox "There is a Disk in drive: " & strDriveName
            End If
        ElseIf (strDrive + ":\") = strDriveName Then
            Res = CheckDrive(strDriveName)
            If Res = CheckDrive_RemAndNotFound Then
                MsgBox "No Disk in drive: " & strDriveName
            ElseIf Res = CheckDrive_RemAndFound Then
                MsgBox "There is a Disk in drive: " & strDriveName
            ElseIf Res = CheckDrive_NotRem Then
                MsgBox "This isn't a removal or CD-Rom drive"
            End If
            DiskFound = True
            Exit For
        End If
        strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
    Next keer
    
    'Check whether there is a drive with the letter the user entered
    If (Not DiskFound) And (strDrive <> "all") Then MsgBox "No drive with this letter found!"
End Function

''''''''''''''''''''''''
'Task:
'1) Check whether the drive is removal or CD-Rom or not
'2) Check if there is a disk in the drive
'The idea depends on changing the directory to the drive
'path then check: if an error happens that means the program
'can't change the directory to the path of the disk.
'Consequently, there is no disk, and the function returns
'
'OutPut of CheckDrive:
'  0 = The drive isn't removal or CD-Rom
'  1 = There is a Disk in the drive
'  2 = There is no Disk in the drive
''''''''''''''''''''''''

Function CheckDrive(strDrive As String) As Integer
    On Error GoTo errhandler
    Select Case GetDriveType(strDrive)
        Case 2  ' Removal
            ChDir strDrive
            CheckDrive = CheckDrive_RemAndFound ' The drive is removal and there is a disk
            Exit Function
        Case 5  ' CD-Rom drive
            ChDir strDrive
            CheckDrive = CheckDrive_RemAndFound ' The drive is CD-Rom and there is a disk
            Exit Function
    End Select
    
Exit Function

errhandler:
    'there isn't a Disk in the drive
    If Err.Number = 75 Then CheckDrive = 2 'Path/access error

End Function

'''try to run this code

Const CheckDrive_NotRem = 0
Const CheckDrive_RemAndFound = 1
Const CheckDrive_RemAndNotFound = 2
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long


'InPut: the letter of the drive to check it or the string "all" to check for all drives
Public Function CheckNow(strDrive As String)
        
    Dim strSave As String
    Dim strDriveName As String
    Dim Res, keer, DiskFound
    
    'Create a buffer to store all the drives
    strSave = String(255, Chr$(0))
    'Get all the drives
    ret& = GetLogicalDriveStrings(255, strSave)
    
    'Extract the drives from the buffer
    For keer = 1 To 100
        If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
        strDriveName = Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
        'Check the drive for type and diskettes
        If strDrive = "all" Then
            Res = CheckDrive(strDriveName)
            If Res = CheckDrive_RemAndNotFound Then
                MsgBox "No Disk in drive: " & strDriveName
            ElseIf Res = CheckDrive_RemAndFound Then
                MsgBox "There is a Disk in drive: " & strDriveName
            End If
        ElseIf (strDrive + ":\") = strDriveName Then
            Res = CheckDrive(strDriveName)
            If Res = CheckDrive_RemAndNotFound Then
                MsgBox "No Disk in drive: " & strDriveName
            ElseIf Res = CheckDrive_RemAndFound Then
                MsgBox "There is a Disk in drive: " & strDriveName
            ElseIf Res = CheckDrive_NotRem Then
                MsgBox "This isn't a removal or CD-Rom drive"
            End If
            DiskFound = True
            Exit For
        End If
        strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
    Next keer
    
    'Check whether there is a drive with the letter the user entered
    If (Not DiskFound) And (strDrive <> "all") Then MsgBox "No drive with this letter found!"
End Function

''''''''''''''''''''''''
'Task:
'1) Check whether the drive is removal or CD-Rom or not
'2) Check if there is a disk in the drive
'The idea depends on changing the directory to the drive
'path then check: if an error happens that means the program
'can't change the directory to the path of the disk.
'Consequently, there is no disk, and the function returns
'
'OutPut of CheckDrive:
'  0 = The drive isn't removal or CD-Rom
'  1 = There is a Disk in the drive
'  2 = There is no Disk in the drive
''''''''''''''''''''''''

Function CheckDrive(strDrive As String) As Integer
    On Error GoTo errhandler
    Select Case GetDriveType(strDrive)
        Case 2  ' Removal
            ChDir strDrive
            CheckDrive = CheckDrive_RemAndFound ' The drive is removal and there is a disk
            Exit Function
        Case 5  ' CD-Rom drive
            ChDir strDrive
            CheckDrive = CheckDrive_RemAndFound ' The drive is CD-Rom and there is a disk
            Exit Function
    End Select
    
Exit Function

errhandler:
    'there isn't a Disk in the drive
    If Err.Number = 75 Then CheckDrive = 2 'Path/access error

End Function

''thanks but i want hard disk serial number your code is for drive serial number or volume number.

''thanks but i want hard disk serial number your code is for drive serial number or volume number.

i need code for Hard disk serial number which is not changed after format hard disk.

HDD serial number is displayed in cmos setup also

try this.

'' ABU TAHER again thanks but i want hard disk serial number your code is for drive serial number or volume number.
my mailID rajnew.300@rediffmail.com

i need code for Hard disk serial number which is not changed after format hard disk.
my mailID rajnew.300@rediffmail.com

ok. please try it again.

I need HDD serial Number
in this project after selecting combo box (Slave or master disk) no result display.

you mean no hdd serial number show? if your hdd connect with primary master then it automatically show when you run this project. in my pc it show and it show the hdd serial number what is write on body of hdd. i don't understand what problem with you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.