Results 1 to 8 of 8

Thread: [RESOLVED] help to open usb

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    57

    Resolved [RESOLVED] help to open usb

    hello guys..

    all i want is to open the path of my usb flash disk when the user click the command button..
    but i cant specified the correct drive letter of my flash disk..

    pls help me..
    Last edited by Noob_Coder; Nov 12th, 2008 at 10:40 PM.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: help to open usb

    Welcome to the forums!

    You may want to have a look at this. You could modify it to suit your needs, that is to enumerate just the removable drives for you to determine your USB drive.

    Or you could probably just detect if a USB drive is being inserted and determine its drive letter?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: help to open usb

    i have d same problem..

    thanks for the links..
    Last edited by jp26198926; Nov 11th, 2008 at 10:27 AM.

  4. #4
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: help to open usb

    This will find your drive letter and open a file:
    Code:
    Option Explicit
    
    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    
    
    Private Sub Command1_Click()
        Dim UsbDrive As String
        Dim fnumber As Integer
        Dim myfile As String
        Dim Mydata As String
        
        fnumber = FreeFile
            
         UsbDrive = GetUsbDriveLetter
         
         myfile = UsbDrive & "\temp.txt" 
        Open myfile For Input As #fnumber
         Line Input #fnumber, Mydata
         MsgBox Mydata
        Close #fnumber
            
     
    End Sub
    Public Function GetUsbDriveLetter() As String
         Dim I As Integer
             For I = 65 To 90
             If DriveType(Chr$(I)) = "removeable" Then
               If Chr$(I) <> "A" Then 'A shows as removal
                   GetUsbDriveLetter = Chr$(I) & ":\"
                     
              End If
              
             End If
         Next I
              
        End Function
    Private Function DriveType(sDrive As String) As String
    
              Dim sDriveName As String
              Const DRIVE_TYPE_UNDTERMINED = 0
              Const DRIVE_ROOT_NOT_EXIST = 1
              Const DRIVE_REMOVABLE = 2
              Const DRIVE_FIXED = 3
              Const DRIVE_REMOTE = 4
              Const DRIVE_CDROM = 5
              Const DRIVE_RAMDISK = 6
             
        sDriveName = GetDriveType(sDrive & ":\")   
            Select Case sDriveName
     
                  Case DRIVE_TYPE_UNDTERMINED
                   DriveType = "unknown"
               Case DRIVE_ROOT_NOT_EXIST
                   DriveType = "non-existent"
                Case DRIVE_CDROM
                    DriveType = "CD-ROM"
                Case DRIVE_FIXED
                   DriveType = "local"
               Case DRIVE_RAMDISK
                  DriveType = "RAM"
              Case DRIVE_REMOTE
                   DriveType = "remote"
               Case DRIVE_REMOVABLE
                  DriveType = "removeable"
           End Select
        Exit Function
             
            
      End Function
    Notes:
    Check if file and/or folder exists, more than one flash may be plugged in
    Drive B could also show as removable
    Last edited by isnoend07; Nov 11th, 2008 at 02:29 PM.
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    57

    Re: help to open usb

    thnx to all of u guys....


    to isnoend07:
    Thank you for that code.. it works

    1. may i know why u put "For I = 65 To 90"?
    2. what if there is other flash disk that already plug in?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: help to open usb

    65 to 90 is a to z,
    when i am looking for usb drive i start at the highest drive letter and work backwards as usb drives are normally after all fixed drives and cd drives, i also don't check for drives c: and below

    as isnoend said check some content on the drive (or drive label) to confirm it it is the correct one
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: help to open usb

    Quote Originally Posted by Noob_Coder
    thnx to all of u guys....


    to isnoend07:
    Thank you for that code.. it works

    1. may i know why u put "For I = 65 To 90"?
    2. what if there is other flash disk that already plug in?
    Returns aString containing the character associated with the specified character code.
    Dim MyChar
    MyChar = Chr(65) ' Returns A.

    MyChar = Chr(66) ' Returns B.
    MyChar = Chr(67) ' Returns C.
    etc
    as westconn1 suggested you may want to loop backwards and skip to Chr(68)
    Eg
    For I = 90 To 68


    To check if more than flash is plugged in search for a file or folder that you know exists on each flash if not found you may want to prompt the user and create the folder or file of course then you will have to check for usable space., Like is the Flash drive full. also you could run into the situation where
    the same file or folder is on more than 1 flash drive, then you would also prompt the user to remove one You will also need to check if no flash is plugged in and prompt the user. I have been working on a file backup and restore and am dealing with all these variables. User could also have a bunch
    of flash drives plugged in.
    Good luck
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: help to open usb

    Not exactly sure but you may also want to fiddle with this...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width