Jim Karabatsos - GUI Computing
I'm sure most VB programmers are familiar with the App.Path property, which gives us access to the path from which the program was run. Using this property is a convenient way for your program to locate any support files that it needs. Personally, I tend to place any configuration options that do not depend on the particular user (as well as default values) in a private INI file that lives in the program directory, while I place user-specific options in another private INI file in the user's WIN.INI directory (or the Windows 95 registry).
One thing to be aware of is that the App.Path property may or may not have a trailing backslash. In most cases, it will not have a trailing backslash; however, if the program is run from the root directory of a drive, then it will have the trailing backslash. You might think that it is unlikely that your users will install into a root directory, but it is possible - indeed, in the case of a LAN, it is quite common to map a drive letter directly to an application's home directory.
Being obsessed with catering for all possibilities, no matter how unlikely, I wrote a function that returns the application's path in a predictable format. As the most common requirement is to use the path as a root for other filenames, the function returns the path with a trailing backslash in all cases. As part of this process, I also created a function to do the same to any arbitrary string.
Here are the two functions:
Function sAddBackslashToPath(ByVal sPath_IN As String) _ As String If Right$(sPath_IN,1) = "\" Then sAddBackslashToPath = sPath_IN Else sAddBackslashToPath = sPath_IN & "\" End If End Function Function sAppPath() As String Static sta_sAppPath As String If Len(sta_sAppPath) = 0 Then sta_sAppPath = sAddBackslashToPath(App.Path) End If sAppPath = sta_sAppPath End Function
Notice how we are buffering the result of the sAppPath function in a static variable. Whenever you have a function that returns a constant value, you can trade a little bit of memory for better performance. In this case, you save access to the App.Path property in all but the first time this function is used. I know that this is hardly time-critical code, but optimising these library routines has a high return on investment as they are used in many programs we write.