Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
i need to read S/N of flash memory by Visual Basic . can you help me
please reply me to [DELETED]@irnase.com

[edit]Email removed - OriginalGriff[/edit]
Posted
Updated 8-Apr-12 1:42am
v2
Comments
OriginalGriff 8-Apr-12 7:42am    
Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know

There is a very good chance that this won't work: I have yet to meet a flash drive with a serial number, but maybe that's because I buy cheap ones...

Add a reference to System.Management, then:
VB
Dim all As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'")
For Each mo As ManagementObject In all.[Get]()
	Dim mediaTag As New ManagementObject("Win32_PhysicalMedia.Tag='" + mo("DeviceID") & "'")
	Console.WriteLine(mediaTag("SerialNumber").ToString())
Next
 
Share this answer
 
Here is my method...

Top of code:
VB
' ** Get volume information -
Public Declare Function GetVolumeInformation Lib "kernel32" _
  Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
  ByVal lpVolumeNameBuffer 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


Get Serial Number Function:
VB
Public Function GetSerialNum(ByVal sAppPath As String) As Long
    Dim jSerNum As Long
    Dim sFSNBuff As String
    Dim sSerNum As String
    Dim sVolBuff As String
    Dim sDrvSave As String
    Dim sDirSave As String

    ' ** Init.
    sVolBuff = String(255, 0)
    sFSNBuff = String(255, 0)

    ' ** Make sure sAppPath is set to "x:\".
    sAppPath = Left(sAppPath, 2) & "\"

    ' ** Save info.
    sDrvSave = Left(CurDir(), 2)
    sDirSave = CurDir(sAppPath)

    ' ** Change to the root of sAppPath.
    Call ChDrive(Left(sAppPath, 2))
    Call ChDir(sAppPath)

    ' ** Get serial number for "x:\".
    Call GetVolumeInformation(sAppPath, "", 0, jSerNum, 0, 0, "", 0)

    ' ** If error, get serial number for "x:".
    If jSerNum = 0 Then
        sAppPath = Left(sAppPath, 2)
        Call GetVolumeInformation(sAppPath, sVolBuff, 255, jSerNum, 0, 0, _
          sFSNBuff, 255)
    End If

    ' ** Return to saved directory and drive.
    Call ChDir(sDirSave)

    ' ** Return.
    GetSerialNum = jSerNum
End Function
 
Share this answer
 
Comments
M T Hadi 31-Dec-15 10:24am    
It did work - thank you, but what I suspect one needs is the device factory serial number, which is unchangeable - not the Volume Serial Number, which is editable with the right tools...
VB
Function GetUSBSerialNo(ByVal DriveLetter As String)
        Dim PnPID As String
        PnPID = USBSerialNo(DriveLetter)
     
        If Not Trim(PnPID) = "" Then
            GetUSBSerialNo = formatSerialNo(PnPID)
        Else
            GetUSBSerialNo = ""
       End If
     
    End Function
     
     
    Function USBSerialNo(ByVal DriveLetter As String)
     
    Dim objFSO
    Dim objFolder
    Dim Directory
    Const OverwriteExisting = True
     
    Dim SerialNo As String
     
    Dim ComputerName
    ComputerName = "."
    Dim wmiServices, wmiDiskDrives, wmiDiskDrive, query, wmiDiskPartitions, wmiDiskPartition, wmiLogicalDisks, wmiLogicalDisk
     
    Set wmiServices = GetObject( _
        "winmgmts:{impersonationLevel=Impersonate}!//" _
        & ComputerName)
     
    Set wmiDiskDrives = wmiServices.ExecQuery("SELECT Caption, DeviceID,PNPDeviceID FROM Win32_DiskDrive")
     
    For Each wmiDiskDrive In wmiDiskDrives
     
        SerialNo = wmiDiskDrive.PNPDeviceID '1
     
     query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
            & wmiDiskDrive.deviceid & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
        Set wmiDiskPartitions = wmiServices.ExecQuery(query)
     
        For Each wmiDiskPartition In wmiDiskPartitions
            Set wmiLogicalDisks = wmiServices.ExecQuery _
                ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
                 & wmiDiskPartition.deviceid & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
     
            For Each wmiLogicalDisk In wmiLogicalDisks
     
            If (wmiLogicalDisk.deviceid = DriveLetter) And (wmiLogicalDisk.DriveType = 2) Then '2
                USBSerialNo = SerialNo
                Exit Function
            End If
     
            Next
        Next
    Next
    End Function
     
     
    Function formatSerialNo(ByVal PnPID As String)
        Dim arrSerialNo
        Dim arrSerialNo1
        arrSerialNo = Split(PnPID, "\")
        Dim i
        arrSerialNo1 = Split(arrSerialNo(UBound(arrSerialNo)), "&")
     
        If UBound(arrSerialNo1) > 0 Then
            formatSerialNo = arrSerialNo1(UBound(arrSerialNo1) - 1)
        Else
            formatSerialNo = arrSerialNo1(UBound(arrSerialNo1))
        End If
     
    End Function
     



Private Sub Command1_Click()
MsgBox GetUSBSerialNo(J)
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900