Help with relative project folders

sigilaea

New member
Joined
Jul 24, 2009
Messages
4
Programming Experience
Beginner
PROBLEM #1:
I have a program that accesses several XML files that are located in a subfolder called "xmlfiles." This folder is a subfolder in my project.

I am using "Application.Settings" to store the locations of the XML files. For example, I have an entry for "JobsXML" that has a string with the file path of the XML file to load.

To get these files to load, I had to use relative references: The path settings for "JobsXML" is "..\..\..\xmlfiles\jobs.xml"

I have to go up 3 directories because the files are in "\debug\bin" during design. This will not be the case at runtime.

This arrangement works fine in the editor, but when I make a "ClickOnce" package, the relative path will no longer work for installed applications. The directory will be different.

How can I setup my code so that the "xmlfiles" subfolder will be correctly used at runtime? I need to be able to run this from my project folder under "My Documents/Visual Studio 2005/Projects/etc..." and also, I need it to work from the runtime directory that the user installs the program to.

So if the user installs the package to "C:\temp\" then the code should use the setting for "JobsXML" by going to "C:\temp\xmlfiles\jobs.xml."

PROBLEM #2:
There is an attribute in some of my XML files that is also, a relative file reference.

I have another subfolder called "images" that also resides in my project. This folder contains images that are loaded at runtime.

In my XML file, each node has an attribute, "filepath" that has a value of "..\..\..\images\filename.jpg"

I also need to be able to access this relatively, just like the XMLfiles I mentioned in PROBLEM #1.

I have tried different combinations of StartupPath, Path.Combine and a few others...with no success. I am new to this so I could use some sample code to put this together correctly.

Thanks
 
Last edited:
Copy the files to the Debug-Folder and change the setting.

Bobby

Thanks for the reply. But what settings do I change?

Thanks for the reply. But what settings do I change?

I'm sorry to spam but I think I wasn't clear before. Currently, I have my files in the "\bin\debug" folder and all of my settings use the path "..\..\..\filename.xml"

So I want to know what will I need to change so that it will work when someone installs the application to a folder of their choice?
 
Take a look at the MSDN documentation for Application.StartupPath or Application.StartupUri, pay attention to the link about ClickOnce if that is how you are doing your installation.

You can then manually create a 'Data' directory under \bin\debug and put your files in there.

Then your Application can test for the existence of:

Application.StartupPath & "\Data" and if it doesn't exist, create it and from then on use it for storage/retrieval.
 
You shouldn't be using Application.StartupPath as part of the path where your program will be writing to files because more than likely that'll be in the Program Files directory which applications shouldn't be writing to in the first place.

Use the User's App Data folder instead which can be gotten:
VB.NET:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & System.IO.Path.DirectorySeparatorChar & "YourProgramName"
 
Back
Top