Results 1 to 2 of 2

Thread: Finding exe's......

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Finding exe's......

    I want my program to be able to find exe's, similar to that of Window's Run, so that I don't have to specify which directory the program is in. This eliminates the problem of the Program being opened not being in the default directory and having the user not being able to open the program when they know its there.

    Does anyone know how to do it???

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can use the SearchPath API function.

    The following code will search for an application in the way specified in the comments.
    The Run dialog box however will also check this all keys in the registry under the following path:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
    VB Code:
    1. Private Declare Function SearchPath _
    2.  Lib "kernel32" Alias "SearchPathA" ( _
    3.  ByVal lpPath As String, _
    4.  ByVal lpFileName As String, _
    5.  ByVal lpExtension As String, _
    6.  ByVal nBufferLength As Long, _
    7.  ByVal lpBuffer As String, _
    8.  ByVal lpFilePart As String) As Long
    9.  
    10. Private Function SearchFile(ByVal sFileName As String)
    11.     'This function will search after the file in the following order
    12.     ' 1. The directory the app is loaded from
    13.     ' 2. The active directory
    14.     ' 3. Windows System directory (winnt\system32 or windows\system)
    15.     ' 4. The Windows directory
    16.     ' 5. The directories listed in the PATH environment variable
    17.     Dim sBuffer As String
    18.     Dim nLen As Long
    19.    
    20.     sBuffer = Space$(260)
    21.     nLen = SearchPath(vbNullString, sFileName, vbNullString, _
    22.      Len(sBuffer), sBuffer, vbNullString)
    23.     SearchFile = Left$(sBuffer, nLen)
    24. End Function
    25.  
    26. Private Sub Command1_Click()
    27.     'This is an example on how to call the code.
    28.     'If the file isn't found an empty string is returned
    29.     MsgBox SearchFile("notepad.exe")
    30. End Sub
    Best regards

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