A quick note on App.path

alp0001
03-11-2004, 01:02 AM
A lot of times, members on this forum provide code like the following to get the directory the executable currently resides in:

txtPath = App.path & "\data.txt"

This will work in most cases, but not all. If someone were to run an executable on the root drive, the application would thus fail (at least on some versions of Windows). Here is an example where the app will complain of Run-time error '52': Bad file name or number:

MsgBox "Here is the path: " & App.Path & "\data.txt"
MsgBox Dir$(App.Path & "\data.txt")

There are many ways of fixing this, but the most common way would be using a basic if expression:

Function curDirectory() as String
If Right$(App.path, 1) = "\" Then
curDirectory = App.path
Else
curDirectory = App.path & "\"
End If
' or
curDirectory = App.Path
If Right$(App.Path, 1) <> "\" Then curDirectory = App.Path & "\"
End Function

Now, if you feel adventurous and want to use/try some other methods (for whatever your reasons are), there are some other techniques that have been suggested:

'2) Use an IIF function: look at Note 1
Function curDirectory() as String
curDirectory = IIF(Right$(App.path, 1) = "\", App.path, App.path & "\")
End Function

'3) Provided by gonzo
Function curDirectory() as String
curDirectory = App.Path & Right$("\", Abs(Right$(App.Path, 1) <> "\"))
End Function

'4) Provided by Passel
Dim strAppPath As String

Private Sub Form_Load()
Dim b() As Byte
strAppPath = App.Path
b = strAppPath
If b(UBound(b) - 1) <> 92 Then strAppPath = strAppPath & "\"
End Sub

'5) Provided by Mike_R: look at Note 2
Public oFSO As New FileSystemObject

Sub MySub()
txtPath = oFSO.BuildPath(App.Path, "Data.txt")
End Sub

'6) Provided by FlashFF:
Private Declare Function PathAddBackslash Lib "shlwapi.dll" Alias "PathAddBackslashA" (ByVal pszPath As String) As Long
Function curDirectory() as String
Dim curPath As String
curPath = App.Path + String(1, 0) 'create buffer to allow addition of backslash
PathAddBackslash curPath
If Right$(curPath, 1) = Chr$(0) Then 'check to see if buffer wasn't used
curPath = Mid$(curPath, 1, Len(curPath) - 1)
End If
curDirectory = curPath
End Function

Note 1: A word of warning when it comes to the IIF function: It will evaulate both the true & false parts regardless of the outcome (it will only return one though). Not only that, but it coverts all arguments to variants in order to be evaluated and many pros discourage its use. Try the following for yourself to see it happen:

output = IIf(1 = 2, MsgBox("bad"), MsgBox("good"))

Note 2: Per Mike:
The above requires a Reference to the Microsoft Scripting Runtime library located at C:\Windows\System32\scrrun.dll
I'm sure that the FileSystemObject is wildly slow by everyone's standard around here, but if one is opening/reading/writing files in a tight, repetitive loop, then it will be the actual File-IO maniputation that will eat up your time anyway, not the calls to BuildPath(). Note that I made oFSO external and Public so that one only has to instantiate it once, saving you execution time as well as the the hassle of having to type 'Dim oFSO As New FileSystemObject' for any subsequent uses.
Prevention is the best cure for avoiding crashes. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum