334

I want to create a program that converts files. I would like the user to be able to place the executable file in any directory, and when executing that program (double-clicking on the .exe) I want the program to process all the files within the current folder where the exe file exists. How can the program determine the path in which it is currently executing?

I tried System.Windows.Forms.Application.StartupPath but that seems to be the wrong way.

Any ideas?

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
user2214609
  • 4,713
  • 9
  • 34
  • 41

13 Answers13

472

You should not use Directory.GetCurrentDirectory() in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut.

It's better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); for your purpose. This returns the pathname where the currently executing assembly resides.

While my suggested approach allows you to differentiate between the executing assembly, the entry assembly or any other loaded assembly, as Soner Gönül said in his answer,

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

may also be sufficient. This would be equal to

System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
kmote
  • 16,095
  • 11
  • 68
  • 91
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 12
    GetEntryAssembly() and GetExecutingAssembly() has an interesting difference. For details refer http://stackoverflow.com/a/18562036/30289 – bhadra Nov 08 '13 at 08:42
  • 3
    As I said: This returns the pathname where the *currently executing assembly* resides. The difference between `GetEntryAssembly` and `GetExecutingAssembly` doesn't come as much of a surprise, is also obvious by the function name. If they did the same, why should there be two functions? :-) – Thorsten Dittmar Nov 08 '13 at 08:49
  • 2
    +1 Assembly.GetEntryAssembly() helped me in case of running application via clickonce – Artiom Jan 09 '14 at 13:50
  • 15
    This is in System.Reflection, so `System.Reflection.Assembly.GetExecutingAssembly().Location` is the complete -- if you're testing from Immediate Window – Nate Anderson Dec 16 '15 at 20:32
  • I would also consider `Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).Location)`. This allows getting the directory for any class desired. Just replace `MyClass` with a class contained in the assembly you are looking for. Then it will not matter whether it is the entry or executing assembly. – The Thirsty Ape Jul 27 '23 at 19:34
235
System.AppDomain.CurrentDomain.BaseDirectory

This will give you running directory of your application. This even works for web applications. Afterwards you can reach your file.

hakan
  • 3,284
  • 2
  • 19
  • 25
  • 12
    This is the most accurate property to use for this purpose. The CurrentDomain's BaseDirectory will always point to the correct location even if the AppDomain is dynamically created to look the assemblies from a different location. – dhruvin Apr 21 '15 at 03:05
  • Perfect. This works independently for any application, especially when using Streamreader, which can read different absolute path's depending on the application type. – user3326078 Jan 17 '19 at 23:06
64

I created a simple console application with the following code:

Console.WriteLine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine(System.Environment.CurrentDirectory);
Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
Console.WriteLine(Environment.CurrentDirectory);

I copied the resulting executable to C:\temp2. I then placed a shortcut to that executable in C:\temp3, and ran it (once from the exe itself, and once from the shortcut). It gave the following outputs both times:

C:\temp2
C:\temp2\
C:\temp2
C:\temp2
C:\temp2

While I'm sure there must be some cockamamie reason to explain why there are five different methods that do virtually the exact same thing, I certainly don't know what it is. Nevertheless, it would appear that under most circumstances, you are free to choose whichever one you fancy.

UPDATE: I modified the Shortcut properties, changing the "Start In:" field to C:\temp3. This resulted in the following output:

C:\temp2
C:\temp2\
C:\temp3
C:\temp3
C:\temp3

...which demonstrates at least some of the distinctions between the different methods.

kmote
  • 16,095
  • 11
  • 68
  • 91
  • 1
    An important note. The working directory is not always going to be the same as the installed directory. – Jordan Dec 23 '19 at 13:59
29

Try this:

System.Environment.CurrentDirectory

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
27

Use this,

var currentDirectory = System.IO.Directory.GetCurrentDirectory(); 

You can use this as well.

var currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
kmote
  • 16,095
  • 11
  • 68
  • 91
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
23
string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

From Path.GetDirectoryName

Returns the directory information for the specified path string.

From Application.ExecutablePath

Gets the path for the executable file that started the application, including the executable name.

kmote
  • 16,095
  • 11
  • 68
  • 91
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
18

for .NET CORE use System.AppContext.BaseDirectory

(as a replacement for AppDomain.CurrentDomain.BaseDirectory)

Yonatan
  • 323
  • 3
  • 10
7

1.

Directory.GetCurrentDirectory();

2.

Thread.GetDomain().BaseDirectory

3.

Environment.CurrentDirectory
Tomtom
  • 9,087
  • 7
  • 52
  • 95
2

This block of code makes a path of your app directory in string type

string path="";
path=System.AppContext.BaseDirectory;

good luck

1

This works best for me, especially when using dotnet core single file publish. Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).

Keisha W
  • 686
  • 6
  • 17
0

Use Application.StartupPath for the best result imo.

farosch
  • 210
  • 4
  • 16
0

If you want the exe path you can use System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

Ashiquzzaman
  • 5,129
  • 3
  • 27
  • 38
Howard Rothenburg
  • 1,220
  • 1
  • 11
  • 7
0

If you want the path of the project folder you also can do this:

        DirectoryInfo di = new DirectoryInfo(".");
       //OR 
        DirectoryInfo di = new DirectoryInfo("../../../");

        Console.WriteLine(di.FullName);

Result: D:\Dev\C#\ProjectFolder\ProjectFolder\

Dieally
  • 1
  • 1