85

I tried to get the directory of the console application using the below code,

Assembly.GetExecutingAssembly().Location

but this one gives me where the assemble resides. This may be different from where I executed the application.

My console application parses logs with no parameters. It must go to the logs/ folder inside of the executable's folder or if I give it a path to logs/ it parses it.

2
  • Have a look at this question: stackoverflow.com/questions/674857/… Commented Oct 3, 2014 at 6:40
  • you want the myapp.exe that you double clicked on to run, right? not the dlls?? try GetEntryAssembly Commented Mar 1, 2022 at 10:28

6 Answers 6

130

Use Environment.CurrentDirectory.

Gets or sets the fully qualified path of the current working directory.
(MSDN Environment.CurrentDirectory Property)

string logsDirectory = Path.Combine(Environment.CurrentDirectory, "logs");

If your application is running in c:\Foo\Bar logsDirectory will point to c:\Foo\Bar\logs.

Sign up to request clarification or add additional context in comments.

3 Comments

Current Directory is different from the directory containing the current executable.
Environment.CurrentDirectory is a value that can and will change through the course of running your application. For instance, using default parameters, the OpenFileDialog in WinForms will change this value to the directory where the file was selected from
@KFL Yes, but the OP seems to want the dir of where the exe was executed. So this is the correct answer...
54

Use this :

System.Reflection.Assembly.GetExecutingAssembly().Location

Combine that with

System.IO.Path.GetDirectoryName if all you want is the directory.

2 Comments

If I use Environment.CurrentDirectory which run under Scheduled Task, it will goes to C:\Windows\system32\log which is not the execution path. Then I use this System.Reflection.Assembly.GetExecutingAssembly().Location, it points to my correct exe execution path. Thanks!
This might not work as you hope in unit tests or web projects running local IIS. The CodeBase answer does.
22

Scott Hanselman has a blog post with the following:

using System;
using System.Diagnostics;
using System.IO;
 
namespace testdir
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"Launched from {Environment.CurrentDirectory}");
            Console.WriteLine($"Physical location {AppDomain.CurrentDomain.BaseDirectory}");
            Console.WriteLine($"AppContext.BaseDir {AppContext.BaseDirectory}");
            Console.WriteLine($"Runtime Call {Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)}");
        }
    }

which he uses in .NET Core 3.1, however, I'm running .NET 4.8 and it's working for me.

Comments

8

Safest way:

string temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

2 Comments

The CodeBase is a URL to the place where the file was found, while the Location is the path from where it was actually loaded. For example, if the assembly was downloaded from the internet, its CodeBase may start with "http://", but its Location may start with "C:\". If the file was shadow copied, the Location would be the path to the copy of the file in the shadow-copy dir. It’s also good to know that the CodeBase is not guaranteed to be set for assemblies in the GAC. Location will always be set for assemblies loaded from disk, however.
Being sure the file is on disk, I use this version to get a "regular" path instead of a file:// path: Path.GetDirectoryName(new Uri(this.GetType().Assembly.GetName().CodeBase).LocalPath)
4

Here is a simple logging method

using System.IO;
private static void logWrite(string filename, string text)
{
    string filepath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + filename;

    using (StreamWriter sw = File.AppendText(filepath))
    {
        sw.WriteLine(text);
        Console.WriteLine(text);
    }
}

Usage:

logWrite("Log.txt", "Test");

Comments

2

If google takes in here in 2024 using dotnetcore 8.0, the current way is

System.AppContext.BaseDirectory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.