Results 1 to 4 of 4

Thread: Identifying drive letter of a VB exe

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    2

    Question

    Is there a way to get an .exe that is on a CD-ROM to determine the letter of the drive that it is in? I just need the drive letter, no other info. This is in order to run the program entirely from the CD with no setup.exe or install.

    Thanks

  2. #2
    Lively Member
    Join Date
    Jul 2000
    Posts
    82
    hi, first of all, you must know that vb programs that have been compiled into app.exe need certain dll files. you can download a setupkit that installs them automatically, in the microsoft site(www.microsoft.com).
    secondly, you can do this in many ways. here's one of them:
    1.
    in the app.exe you can add this code and get the drive letter in an integer named "DrvLett":

    dim DrvLett as integer

    DrvLett = app.Path
    DrvLett = mid(DrvLett,1,3)

    now if the applications path is : "d:\windows\app\app.exe then DrvLett sould be : "d:\"
    hope it works out, bye.

  3. #3
    Guest
    Create a module and use this code

    Code:
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    
    Public Function GetDrives() As Variant
        Dim sDrive() As String
        Dim sDrives As String
        Dim nDrives As Long
        
        sDrives = Space(255)
        sDrives = Left(sDrives, GetLogicalDriveStrings(255, sDrives))
        While Len(sDrives)
            ReDim Preserve sDrive(nDrives)
            sDrive(nDrives) = Left(sDrives, 3)
            nDrives = nDrives + 1
            sDrives = Mid(sDrives, 5)
        Wend
        GetDrives = sDrive
    End Function
    Then create a function in your VB program that will search for the specific file you are looking for.

    Code:
    Private Function foo()
        Dim FileSystemObject As Object
        
        Dim vDrives As Variant
        Dim nDrive As Long
        Dim temp As String
        vDrives = GetDrives
        Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
        For nDrive = 0 To UBound(vDrives)
            temp = vDrives(nDrive) & "filename.exe"
            
            If FileSystemObject.FileExists(temp) Then
                
                foo = vDrives(nDrive)
                Exit For
            End If
    
        Next
    End Function
    the Drive you are looking for will be returned by foo()

    [/CODE]

  4. #4
    Guest
    Here's how to detect if there's a CD in the CD-Rom and it's drive letter.

    Code:
    Const DRIVE_CDROM = 5
    
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    
    Private Sub Command1_Click()
    Dim tmp As Integer
    Dim tmpStr As String
    Dim Drives As String
    Dim CDsCount As Integer
    Dim CDsLetters As String
    
    'init Drives to 255 spaces
    Drives = Space(255)
    'get drives, Drives var will look like
    '   A:\<NULL>C:\<NULL>D:\<NULL>E:\<NULL><NULL>
    'ret& is the new length of Drives
    ret& = GetLogicalDriveStrings(Len(Drives), Drives)
    For tmp = 1 To ret& Step 4
     'get a drive root directory (like "C:\")
     tmpStr = Mid(Drives, tmp, 3)
     'if drive is a CD
     If GetDriveType(tmpStr) = DRIVE_CDROM Then
      CDsCount = CDsCount + 1
      CDsLetters = CDsLetters & Left(tmpStr, 1) & " "
     End If
    Next tmp
    'display results
    If CDsCount Then
     MsgBox "Number of CDs Available: " & CDsCount & " - CDs Letters: " & UCase(CDsLetters)
    Else
     MsgBox "No CDs Available"
    End If
    End Sub

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