71

From program a.exe located in c:/dir I need to open text file c:/dir/text.txt. I don't know where a.exe could be located, but text.txt will always be in the same path. How to get the name of the currently executing assembly from within to program itself so that i can access the text file?

EDIT: what if a.exe is a Windows service? It doesn't have Application as it is not a Windows Applicaion.

starball
  • 20,030
  • 7
  • 43
  • 238
pistacchio
  • 56,889
  • 107
  • 278
  • 420

7 Answers7

141

I usually access the directory that contains my application's .exe with:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Sander
  • 25,685
  • 3
  • 53
  • 85
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
  • 2
    Me too, and having seen the other answers I now feel like I've been missing a trick! – Simon Steele Aug 03 '09 at 12:55
  • 6
    For those trying this above line out in LINQPad and getting a NullReferenceException, `Assembly.GetEntryAssembly()` returns null in LINQPad but in your own .NET application it should return a non-null value. – Allon Guralnek Dec 18 '12 at 08:56
  • 14
    Beware that this code does not work if it's executed inside a MS test: in that case, GetEntryAssembly() returns null and the code fails. – sthiers Feb 05 '14 at 14:35
  • See this post for getting this to work in production and unit tests: http://www.codeproject.com/Questions/334267/How-do-I-do-Unit-Testing-with-System-Reflection-As – Aaron Oct 30 '14 at 18:01
  • 6
    I solve the unit test problem with `Assembly assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();` – Nigel Touch Oct 13 '16 at 18:21
  • How do you mock this? If I am trying to test my unit tests will look in a different folder. – Lostaunaum Jul 31 '17 at 21:22
16
string exePath = Application.ExecutablePath;
string startupPath = Application.StartupPath;

EDIT - Without using application object:

string path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

See here for more info:

http://msdn.microsoft.com/en-us/library/aa457089.aspx

Winston Smith
  • 21,585
  • 10
  • 60
  • 75
  • 3
    Just want to point out that the `Application` type is located in the `System.Windows.Forms` namespace - see: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath(v=vs.110).aspx – Lasse Christiansen Feb 27 '14 at 14:51
  • 1
    What about WPF? , Application does not contains a StartupPath method??!! – abdou_dev Sep 28 '20 at 13:02
5

Application.ExecutablePath

Application.StartupPath

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Tommy Carlier
  • 7,951
  • 3
  • 26
  • 43
4

Get the assembly you are interested in (eg. assigned to a System.Reflection.Assembly a variable):

  • System.Reflection.Assembly.GetEntryAssembly(), or
  • typeof(X).Assembly for a class X that's in the assembly you're interested in (for Windows Forms you could use typeof(Program))

Then get the path of where the file from which that assembly a was loaded from:

  • System.IO.Path.GetDirectoryName(a.Location)

The Application object from a Windows Forms application is also a possibility, as explained in other answers.

peSHIr
  • 6,279
  • 1
  • 34
  • 46
3

In VB.NET we can get it in following way:

Assembly.GetEntryAssembly.Location

In C#:

Assembly.GetEntryAssembly().Location
Ciaran Gallagher
  • 3,895
  • 9
  • 53
  • 97
maks
  • 129
  • 1
  • 5
1

using peSHlr's answer worked well when testing in NUnit as well.

var thisType = typeof(MyCustomClass);

var codeLocation = Path.GetDirectoryName(thisType.Assembly.Location);

var codeLocationPath = Path.GetDirectoryName(codeLocation);

var appConfigPath = Path.Combine(codeLocationPath, "AppConfig");
dynamiclynk
  • 2,275
  • 27
  • 31
0
MessageBox.Show("This program is located in: " + Environment.CurrentDirectory);
jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • 28
    Since you were down voted (not by me), you might like to know why. The current directory property can get set in code, so this can change. This can also be different if the application is started from a shortcut. – Kleinux Aug 07 '09 at 19:22
  • 1
    To anyone seeing this in the future, let's agree to keep this here as an example of what not to try and why, and let's agree to upvote jay's humble reply to at least the number of times this gets downvoted, since at least he recognizes the reason, now, that it doesn't work. :) – vapcguy Nov 17 '17 at 23:54