Hello everybody
How to show usb drive letter when we attach my usb to my computer
example:
we use a label or textbox and one timer
now we want to do this we start my project and attach a usb drive to my pc
the lebel or textbox show much fast my usb storage device drive letter ( I:\ or f:)
please tell me a good solution

Dani AI

Generated

Nice start by . The slowdown you saw, , is from polling every tick with FileSystemObject. You do not need a Timer. Let the SysInfo control raise the plug/unplug events, then do a tiny, fast scan using Win32 APIs. That avoids the scripting runtime and keeps the UI snappy.

Drop the Timer and paste this on your form (keep your Label and SysInfo control):

Option Explicit

' Win32 API to list and classify drives
Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nRootPathName As String) As Long

Private Const DRIVE_REMOVABLE As Long = 2   ' USB flash, card readers, etc.

Private Sub SysInfo1_DeviceArrival(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
    Label1.Caption = "Drive: " & FirstRemovableLetter()
End Sub

Private Sub SysInfo1_DeviceRemoveComplete(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
    Label1.Caption = "Drive: -"
End Sub

Private Function FirstRemovableLetter() As String
    Dim mask As Long, i As Long, root As String
    mask = GetLogicalDrives()                 ' bitmask A:=bit0, B:=bit1, ...
    For i = 0 To 25
        If (mask And (2 ^ i)) <> 0 Then
            root = Chr$(65 + i) & ":\"
            If GetDriveType(root) = DRIVE_REMOVABLE Then
                FirstRemovableLetter = Left$(root, 2)
                Exit Function
            End If
        End If
    Next
    FirstRemovableLetter = "-"
End Function

Notes:

  • Some USB hard disks report as fixed. If you want any newly attached storage, remove the GetDriveType check and just show the most recent letter you care about.
  • About Avast: portable VB6 toolchains can trigger heuristics. Use your full VB6 install (as you planned), avoid EXE packers, and consider code-signing your build. If it still flags, submit the EXE to Avast as a false positive and they usually correct it in their definitions soon after.

Recommended Answers

All 4 Replies

Add Reference : Microsoft Script Runtime
Add 1 label.

Private Sub SysInfo1_DeviceArrival(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
Call Check
End Sub

Private Sub SysInfo1_DeviceRemoveComplete(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
Call Check
End Sub

Private Sub Check()
Dim FSO As New Scripting.FileSystemObject, drv As Scripting.Drive
'No Usb Detected
Label1.Caption = "Drive : -"

For Each drv In FSO.Drives
    If drv.IsReady Then
        If drv.DriveType = Removable Then
            Label1.Caption = drv.DriveLetter & ":"

        End If
    End If
Next
End Sub

Private Sub Timer1_Timer()
Call Check
End Sub

Thanks Jxman code is right work but i have a problem please tell me whats wrong
when we start project the system work very slowly and second problem
we use a portable vb6 in home and use a full installation software of vb6 in my work shop
when we use a portable vb6 at home and when we start a project or compile any application the avast
antivirus say it is a malicious program and close the program
by the way this code is work just you reply what i do and we mark solve this thread
Thank Jxman

I'm currently using Avast antivirus and there's nothing alert when i run the compiled application.
I don't know if this happen caused by portable vb6 since i'm using the full installation.

Ok Don't worry
the we use this code in full installation vb
Thanks

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.