1

I created a program in C# .net using VS2008.

When the user installs the program I don't know if it will be on the C: drive or D: drive or someplace else. The issue is that I need the program to automatically create files and then retrieve those files later on without any assistance from the user.

I thought the least complex situation was to place these files in the directory where the program is installed so that the location would remain constant and this strategy would reduce the chances that the files will be accidentally deleted by the user.

Question 1:

How to I refer to the install directory of my app when the user has their choice of the install location?

Question 2:

Should I even be doing it this way or should I be looking at this from a totally different perspective.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
JK.
  • 1,144
  • 3
  • 15
  • 32

5 Answers5

3

Assembly.GetExecutingAssembly().Location will give you the path to the currently executing assembly.

However, writing to that location will cause problems for users running on Vista, Server 2008 and later, which are more strict about normal users not writing to places like C:\Program Files. Your best bet is probably something like:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

There are a bunch of options for Environment.SpecialFolder, based on what exactly you want to store.

Zack Elan
  • 1,716
  • 2
  • 16
  • 23
1

Sounds like a perfect solution for the Isolated Storage function in .Net (System.IO.IsolatedStorage).

You access via objects and the path is controlled by .Net. It stores the items in the local settings\application data\isolated storage for each log in.

Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
0

You can use the System.Environment.CurrentDirectory property to determine where your application belongs.

Alternatively, you could look into isolated storage. The data is stored on a user/machine basis (as you choose) so you can store different files for different users.

Which you choose depends on how much information you want to store, and the security surrounding it.

Russell
  • 17,481
  • 23
  • 81
  • 125
  • System.Environment.CurrentDirectory doesn't give the install directory, it gives the current active/working directory. See http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx – OJ. Aug 12 '09 at 03:01
0

You can get the current directory using:

string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

This is a valid and useful way of storing files related to your .exe - you can always navigate using relative directories from where your executable is running from, meaning you don't need to worry about where it's installed.

Alternately, if you'd like your .exe to be freely copyable to any directory, you could consider storing your data in a well known location, like the user's Application Settings directory in their user profile (for windows).

womp
  • 115,835
  • 26
  • 236
  • 269
0

Application.StartupPath should do that for you or:

private static string GetApplicationPath()
{
    return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}

How to get the path of app(without app.exe)?

Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184