Click here to Skip to main content
15,912,756 members
Articles / Desktop Programming / Windows Forms
Article

How to persist DIRs past SETUP wizard

Rate me:
Please Sign up or sign in to vote.
1.29/5 (3 votes)
30 Apr 20072 min read 18K   9   2
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


Written By
Web Developer
United States United States
Ugly Old Fat Guy (grin).

Comments and Discussions

 
GeneralSo Pin
maabd9-May-07 0:09
maabd9-May-07 0:09 
GeneralRe: So Pin
KinnMirr9-May-07 4:37
KinnMirr9-May-07 4:37 
You do NOT get the source code (unless you've ALSO set "Copy To Output Directory" for EVERY source code file). The only files automatically copied are those marked as "Copy To Output Directory". And they're copied and re-copied every time you run the application in the IDE.

FlashSand

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.