Windows Develop Bookmark and Share   
 index > ClickOnce and Setup & Deployment Projects > In a setup project, how to save a filename based on user selected installation directory?
 

In a setup project, how to save a filename based on user selected installation directory?

I have a Setup Project to install my main project. In my main project, I have a settings file that stores the filename of a file I need to use. Currently, I have it set to "Directory\Filename" that is relative to the location of the .exe. This works fine, but there are times when the "Environment.CurrentDirectory" gets changed and the relative path no longer works. Is there a way to set this filename to the absolute filename during/after installation such that it prepends the user selected installation directory? Is it possible I'm going about this the wrong way/is there a different way to tackle this problem?

Thanks,
Ryan
oryan_dunn  Wednesday, April 30, 2008 1:48 PM

I always use relative paths (so the directory can be moved around or installed anywhere) and then use Application.StarPath to build an absolute path from my relative path.

/Ruben

RJJournal

maxruben  Wednesday, April 30, 2008 2:13 PM

I think what you're looking for is the IsolatedStorage classes. Those can handle dynamically user directories specific to the application, in all OSs that support .NET.

http://www.ondotnet.com/pub/a/dotnet/2003/04/21/isolatedstorage.html

David M Morton  Wednesday, April 30, 2008 1:52 PM

If the paths are chaging all the time you can do something like this: Have an xml file that contains the path.

private string myLocation;

public MyConstructor()

{

GetMyFileName();

}

private void GetMyFileName()
{
XmlDocument pathDoc = new XmlDocument();
pathDoc.Load(Application.StartupPath + @"\MyFileName.xml");
XmlNode node = pathDoc.SelectSingleNode("Path");
myLocation = node.InnerText.Trim();
}

The Xml File looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Path>C:\My Documents\myFile</Path>

The only downfall, is if the customer is moving it around, he/she will have to remember to change the path in the XML.

JohnGrove  Wednesday, April 30, 2008 1:58 PM

I always use relative paths (so the directory can be moved around or installed anywhere) and then use Application.StarPath to build an absolute path from my relative path.

/Ruben

RJJournal

maxruben  Wednesday, April 30, 2008 2:13 PM
Thanks for all the suggestions. I'll look into the IsolatedStorage class a bit more. Just a little more detail on my situation; I've got several files that I ship with my app. I allow the user to use the OpenFileDialog to select a different file to use, which changes the Environment.CurrentDirectory. So if they only change one file, then the others that are relative no longer work. My original plan was to reset the Environment.CurrentDirectory after the OpenFileDialog so the relative paths would work. Is there a way to detect if a path is relative or absolute? If so, I could manually append the Application.StartPath to those files that are still relative.

Thanks,
Ryan
oryan_dunn  Wednesday, April 30, 2008 2:42 PM

Take a look at Path.IsPathRooted().

/Ruben

RJJournal

maxruben  Wednesday, April 30, 2008 6:30 PM
The application folder in a Setup&Deployment project is identified by the TARGETDIR property. So you could create a registry data item with the value [TARGETDIR] (and that's case-sensitive) and thebrackets make it resolve to the actual directory even if the user changed it. So if you use this path and your relative entry together you should find the file ok.

PhilWilson  Wednesday, April 30, 2008 8:19 PM

You can use google to search for other answers

Custom Search

More Threads

• Application Updater Component / Apache
• How to prevent data loss in the registry during upgrade with MSI?
• IIS Frontpage Extension, Sharepoint & ClickOnce
• vista folder permission
• Trouble with "InstallAllUsers".
• Update app via website
• Is it Okay to have a Publish Status of Include for Referenced Components as far as Possible
• + Cannot make this application an online application because the previous version is installed.
• A different MSI/Vista/UAC/2869 problem
• Unable to delete corrupted ClickOnce cache - what's file-locking it?