Resolved .InitialDirectory = Application.StartupPath Not What I Thought It Was

zunebuggy65

Active member
Joined
Oct 12, 2023
Messages
42
Programming Experience
3-5
So I created a very quick and basic application just to see how it would install. I did Build and also selected a temporary certificate from file.

I installed it. I have a button that loads an image file into a PictureBox. In my app, I have the initial directory set like this:
VB.NET:
opnImage.InitialDirectory = Application.StartupPath
.

I am guessing this is incorrect since when I click to load the image (after the application is installed on my PC) the default directory is unrecognizable. It is:
application_location.png


That is not at all what I expected as my Application.StartupPath. I don't might having an ini file to load/save settings in AppData but I really expected a more recognizable path for browsing for a file. I guess I chose this because I used to use VB6 and app.path was what we used and Application.StartupPath seemed close to that.

Can anyone tell me what to use for the InitialDirectory so it will make sense to a user that installs the application?

Thank you.
 
If you are publishing your app using the functionality built into VS then you are using ClickOnce. ClickOnce apps are installed in the ClickOnce cache, so they can be managed by ClickOnce without falling afoul of Windows permissions. What you're seeing there is the path of the ClickOnce cache folder for that app, and that is exactly what you should expect for ClickOnce apps. That is the folder that your EXE is installed in so that is also the folder in which any content files will be stored, so that is the folder you should be using to access such files. The whole point of Application.StartupPath is that it will get the path of the folder containing the program, no matter where that is.

What you probably ought to be doing is using one of the standard Windows file folders intended for particular purposes rather than the program folder, especially if you're going to let the user save and delete files. Checkout My.Computer.FileSystem.SpecialDirectories to see what's available and do some reading to see what each folder is for and then decide which you should be using.
 
Thank you.
VB.NET:
y.Computer.FileSystem.SpecialDirectories
was exactly what I needed.

I also got the Microsoft Visual Studio Installer Project and that works better than the ClickOnce installer. I did install my test MSI on my wife's computer and found out I need to invest in a install certificate or get a "We've protected your PC" warning. That's an expense I was not aware of, but I guess there is no way around it.
 
Back
Top