Help with project path

Peter King

Member
Joined
Sep 11, 2006
Messages
15
Programming Experience
Beginner
Hi,

My program needs to determine the directory in which the application's executable and associated files are found. I use the following on loading the start form:

CurrentDir = System.IO.Directory.GetCurrentDirectory()

to get the directory path. However, it works only when the executable is run from the directory it resides. If the executable is put on the desktop or anywhere that is out of its directory, it does not work.

Anyone can help please?

Peter
 
John,

Thank you very much for the reply.

I do mean running different copies of the same executable that are in different locations (e.g. desktop, a folder on c drive).

I tried the following (maybe indirect, unclever way) to find the original path (folder) of the excecutable which shares the same path as an indicator file "exepath". This "exepath" file is supposed to be unique in the computer. Again it works when the executable is run from the original folder (location), but not on desktop.

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
For Each d In allDrives
If d.DriveType = DriveType.Fixed Then 'Just searching hard disks 
Dim drive As String = d.Name.Substring(0, d.Name.Length - 1)
For Each foundFile As String In _
My.Computer.FileSystem.GetFiles(drive, _
FileIO.SearchOption.SearchAllSubDirectories, "exepath")
Dim tem() As String = Split(foundFile, "\")
CurrentDir = foundFile.Substring(0, foundFile.Length - _
tem(tem.Length - 1).Length - 1)
Return
Next
End If
Next
End Sub
When run the executable from its original directory, "CurrentDir " returns the correct path. But when a copy of teh executable on the desktop is run, file "exepath" is not found!

Please help!
 
Last edited by a moderator:
I'm afraid I don't understand. Different copies of an executable run from different locations is considered different applications and not in any way related. Application.StartupPath will tell you the path of any currently used application (regardless of what shortcut location the application was started from).
 
I may not made myself clear.

I have a VB exe file which is associated with several other files. All these files (exe and associated files) are in the same directory. When the exe file is executed in this original location, the application path is determined simply by:

Application.ExecutablePath

I need this path so that the associated files can be found and manipulated.

However, when I put the exe file on the desktop, the above approach to find the path of the associated files is not working. All I need is a way to determine the path of the associated files no mather where the exe file is put and executed.
 
When you simply put the executable at a random location you have broken thte 'link', the other files are not associated anymore unless you also copy these along. Use ClickOnnce or other installer to install the application. If you need to install it at several locations you could perhaps do so. If you need to start one single installed application from several location you simply create a new short to it.
 
Thanks a lot for the reply.

I think the best way of dealing this is to store the path of the application in my.settings at installation time.

I have a setup project in the solution, which generates a *.msi. When a user installs the program from *.msi, he/she can choose the path where the exe and associated files will be installed. Is it possible to store that user defined path in my.settings? If yes, how?

if that user defined path is store in my.settings, than no matter where the exe file copies are sitting, executing them should work.
 
I don't think my.settings is appropriate for this because it is linked to the installed application or application+user. If you move the executable you break the installation. A Registry setting or some other storage location that doesn't relocate could work.
 
Thanks a lot for the suggestion.

Because I am new to VB programming, could you show me details of how to store the user defined path (determined during installation) in the registry setting, please?
 
Somewhere in this article there is description and screenshot about the [TARGETDIR] registry setting for setup projects.
 
That article is of great help to me! I will read it and follow it. I may come back to you to seek further assistance if I cannot get it working.
 
Hello, John,

That article suggested by you helped me solving the problem nicely. Thank you so much for this.

Now I have a small associated problem. I generated a 16*16 icon and 32*32 icon. They are assigned to my executable and shortcuts. These icons show them correctly for the executable file, and for the shortcut (to the exe) shown in the programs menu. However, when the shortcut is put on the desktop, the icon does not show itself. Do you know what is wrong?

Thanks.

P
 
How is the shortcut put on desktop?
 
I add a shortcut to the User's Desktop folder in File System on Target Machine of the setup project. I then right click the shortcup to set the icon for the shortcut. The icon is in the Application Folder of the setup project.
 
Back
Top