Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

How to persist DIRs past SETUP wizard

1.29/5 (3 votes)
30 Apr 20072 min read 1  
How to Find Directory Path before and after setup wizard deployment

Introduction

Using .NET ... how do you find your support directories and files before and after Setup Deployment.

Let's say ... you've got a .NET WinForm app. The app uses several support files which must be installed during setup. You're using a standard 2005 setup project.

Do you relize how "tricky-cool" the IDE really is?
Ok, here's how it works.

Application.ExecutablePath returns either "MyProject/bin/debug" or "MyProject/bin/release" depending on the build configuration. BUT ... once MSI wrapped and installed then Application.ExecutablePath returns "ProgramFiles/Manuf/MyProject".

So, the blasted PATH CHANGES! Meaning, any subdirectory/file you wanted to FIND during development and ALSO find after deployment ends up in THREE POSSIBLE PLACES!!!! RATS ...

Any file (or subdirectory) you want to be included in the final SETUP msi needs to have it's "Build Action" property set to "Content" then all the subdirectories-and-files are copied during setup to the installer [TargetDirectory].

HOWEVER, and here's the TRICKY part, there's another property for each file called "Copy To Output Directory" which needs to be set to "CopyAlways" ... then ... everytime you run the app in the IDE, the debugger FIRST copies all the files so marked into the appropriate "bin/debug" or "bin/release" directory BEFORE RUNNING! Every time you run! Every time it copies! So, using Application.ExecutablePath always works! While using debug in the IDE and after deployment.

Using the code

Here's how I do it now ...
I have a static class file called funcs.cs ....

public class funcs
{
     private static string mBasePath="";
     public funcs()
     {
     }
     public static string BasePath
     {
          get { return mBasePath; }
          set { mBasePath = value; }
     }
}

Then ... in my Main initialization, I set the static BasePath ... like this ...

[STAThread] 
static void Main()
{
      fn.BasePath = Application.ExecutablePath.Substring(0, 1 + Application.ExecutablePath.LastIndexOf("\\"));
      Application.Run(new frmMain());
}

And finally, anytime I need to build a path name, I just use BasePath!

PicsHomePath = fn.BasePath + "pics\\";

As long as "Copy To Output Directory" for each file is set to "CopyAlways" then your code FINDS the right path, whether your runing in the IDE or after installation!

Points of Interest

I know, seems simple ... it is simple once you know the "trick"

History

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here