88

Have a look at this pseudocode:

string exe_path = system.get_exe_path()
print "This executable is located in " + exe_path

If I build the above program and place the executable in C:/meow/, It would print out This executable is located in C:/meow/ each time it is run, regardless of the current working directory.

How could I easily accomplish this using C#?

Dante May Code
  • 11,177
  • 9
  • 49
  • 81

9 Answers9

115

MSDN has an article that says to use System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; if you need the directory, use System.IO.Path.GetDirectoryName on that result.

Or, there's the shorter Application.ExecutablePath which "Gets the path for the executable file that started the application, including the executable name" so that might mean it's slightly less reliable depending on how the application was launched.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 30
    System.Reflection.Assembly.GetExecutingAssembly() will only get the EXE assembly if that's where it's called from. GetEntryAssembly() will get the correct assembly. – GraemeF Nov 01 '09 at 22:20
  • 6
    This is especially important if you create a Windows Service because the service is launched from C:\Windows\System32, so you will have that working directory. I will use GraemeF's method instead. – kevindaub Nov 02 '09 at 02:03
  • 56
    The above mentioned method returns the current path as a URI (i.e. "file://c:\\data"). What worked for was: `System.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ) );` – arikfr Feb 01 '10 at 05:49
  • 18
    It's actually "System.IO.Path". So fully qualified name with correct namespace should be: System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location ) – Victor Chelaru Jan 05 '13 at 21:21
  • I'm unclear after these comments which is the right way! Victor's way doesn't include GetEntryAssembly, which according to @GraemeF would be correct. And what's wrong with Application.ExecutablePath? – noelicus Nov 14 '13 at 16:59
  • 10
    noelicus: according to the answer and the comments, the safe and accurate method that will nearly always work correctly is: System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) – Graham Laight Nov 15 '13 at 11:35
  • 1
    `Application.ExecutablePath` is part of Windows.Forms, though... which is not referenced by default in console apps. – Nyerguds May 02 '14 at 13:04
  • The Article is for Compact Framework 1.0. A little out-dated in my opinion. `System.Reflection.Assembly.GetEntryAssembly().Location` should be used for the exe. – Erik Philips May 16 '14 at 05:16
  • 1
    This was maybe correct answer in 2009, but for anything but the most basic Windows hello world app correct answer is the one by Liquid Core. – zmilojko May 09 '15 at 15:35
65
AppDomain.CurrentDomain.BaseDirectory
Liquid Core
  • 1
  • 6
  • 27
  • 52
  • 9
    This is the best, simplest answer. This method works on linux/mono and Windows, regardless of the current directory. – raider33 Apr 16 '16 at 23:17
13
using System.Reflection;

string myExeDir = new FileInfo(Assembly.GetEntryAssembly().Location).Directory.ToString();
Alex 75
  • 2,798
  • 1
  • 31
  • 48
newb
  • 131
  • 1
  • 2
  • 3
    Nice, though it can be simplified to `System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)`, which is also faster. – mklement0 Jan 15 '19 at 15:21
7
var dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

I jumped in for the top rated answer and found myself not getting what I expected. I had to read the comments to find what I was looking for.

For that reason I am posting the answer listed in the comments to give it the exposure it deserves.

JKennedy
  • 18,150
  • 17
  • 114
  • 198
4

"Gets the path or UNC location of the loaded file that contains the manifest."

See: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx

Application.ResourceAssembly.Location
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • Note that this solution is WPF-specific: "[`Application`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.application) is a class that encapsulates WPF application-specific functionality". – mklement0 Jan 15 '19 at 14:48
2

Suppose i have .config file in console app and now am getting like below.

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\YourFolderName\\log4net.config";
Muhammad Mubashir
  • 1,591
  • 1
  • 21
  • 18
  • 2
    CurrentDirectory is not guaranteed to be the directory in which the application is executing. –  Apr 28 '15 at 15:35
  • 3
    Please use `Path.Combine`. There's a lot of code out there that does not properly work in Mono from manually combined paths like this. – Joseph Lennox Oct 03 '15 at 17:47
2

On my side, I used, with a form application:

String Directory = System.Windows.Forms.Application.StartupPath;

it takes the application startup path.

JYelton
  • 35,664
  • 27
  • 132
  • 191
Sébastien
  • 21
  • 1
2

The one that worked for me and isn't above was Process.GetCurrentProcess().MainModule.FileName.

Charles A.
  • 10,685
  • 1
  • 42
  • 39
2

If you are planning to build a console application to be used with Task Scheduler, I'd recommend using this approach:

var execDirectoryPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)?.Replace("file:\\", "");

This way, the path will adapt to whatever location you place your executable file in.

Joseph L.
  • 431
  • 6
  • 7