0

Possible Duplicate:
C# Executable Executing directory

I used 2010 Visual C# Express to write a little windows form .exe. How do I get the program to "know" what directory it's running from so that it can read/write from/to whichever directory the user has put my .exe in? Basically, I want my little .exe to be "portable", where it doesn't matter which folder/directory it's in, and yet it can "know" where all its files are.

Community
  • 1
  • 1
MrPatterns
  • 4,184
  • 27
  • 65
  • 85

3 Answers3

3

There are different approaches, but this worked for me.

string directory = AppDomain.CurrentDomain.BaseDirectory; 
Batuu
  • 595
  • 1
  • 8
  • 22
3

You can use:

Application.StartupPath

Omar
  • 16,329
  • 10
  • 48
  • 66
aquinas
  • 23,318
  • 5
  • 58
  • 81
0
System.Reflection.Assembly.GetExecutingAssembly().Location
Gerard Sexton
  • 3,114
  • 27
  • 36
  • Careful. In general this will work, but if this code happens to reside in a DLL that is *not* where your main .exe is (example, if this DLL were in the GAC), then you'll get the wrong result. – aquinas Sep 10 '12 at 14:00