Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

App.Path 2

Status
Not open for further replies.

swilliams

Programmer
Aug 23, 2000
583
BM
App.Path returns the folder that the application is installed to, so App.Path & "\test.txt" should return the path and file name of a file with the name test.txt in the folder that your application is installed to.

Unfortunately, this will give errors under certain circumstances.

If for some reason, your user decides to install your application directly onto the root of a drive (c drive for example) App.Path & "\test.txt" will produce an error.

The reason for this is that App.Path returns the path of a folder name that the application is installed to, without a backslash if the application is installed to a folder on a drive, but returns the drive name colon backslash if your application is installed directly onto a drive.

I.E. if your application is installed to c:\program files\your_app_name, App.Path will return
C:\Program files\your_app_name

But if your application is installed directly onto the c drive, App.Path will return
C:\

In this instance, appending "\test.txt" will result in the string
"C:\\test.txt"
giving errors.

For this reason, I code a function called App_Path which is as follows:

Pulbic Function App_Path() As String
If Right(App.Path, 1) = "\" Then
App_Path = App.Path
Else
App_Path = App.Path & "\"
End If
End Function

and the I use App_Path & filename.extension to avoid these errors.

Simon
 
Good point Simon! Another consideration for using App.Path when running on a networked computer is to make sure App.Path doesn't return a UNC path ("\\MyServer\MyApps\..."). Depending on how the program is launched [tt]ChDrive App.Path[/tt] and [tt]ChDir App.Path[/tt] can result in fatal runtime errors. It's much safer to maintain the location of a "data folder" in the registry, retrieving that information every time a user starts the application. I've used variations on the following code several times:
[tt]
Private Sub Form_Load()
Dim DataFolder As String
'DataFolder would best be declared at the module level
DataFolder = GetSetting(appname:=App.EXEName, _
section:="Startup", _
Key:="StartFolder", _
Default:="Value Not Set")
If DataFolder = "Value Not Set" Then
If MsgBox("This is the first time " _
& App.EXEName & "has been run on this computer." _
& vbCrLf _
& "Do you want to store program settings and data in " _
& App.Path & "?", vbYesNo) = vbYes Then
SaveSetting appname:=App.EXEName, _
section:="Startup", _
Key:="StartFolder", _
Setting:=App.Path
Else
'Insert code to allow the user to create or browse for a folder
End If
Else
MsgBox "Data for " & App.EXEName _
& " is stored in " & DataFolder & "."
End If
DataFolder = GetSetting(appname:=App.EXEName, _
section:="Startup", _
Key:="StartFolder", _
Default:="Value Not Set")
If DataFolder = "Value Not Set" Then
ret = MsgBox("You must specify a data directory." _
& vbCrLf & "Click okay to exit.", vbCritical)
'insert code to unload any objects already loaded
End
End If
If Dir(DataFolder, vbDirectory) = "" Then
m = MsgBox(DataFolder & " isn't a valid folder!", vbCritical)
End
End If
End Sub
[/tt]

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top