How to get execution directory of console application
Asked Answered
E

5

82

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.

Eppie answered 12/6, 2012 at 9:19 Comment(2)
Have a look at this question: #675357Millibar
you want the myapp.exe that you double clicked on to run, right? not the dlls?? try GetEntryAssemblyDelphine
S
129

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.

Skirl answered 12/6, 2012 at 9:21 Comment(3)
Current Directory is different from the directory containing the current executable.Recursion
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 fromYuu
@Recursion Yes, but the OP seems to want the dir of where the exe was executed. So this is the correct answer...Unfetter
G
53

Use this :

System.Reflection.Assembly.GetExecutingAssembly().Location

Combine that with

System.IO.Path.GetDirectoryName if all you want is the directory.
Gully answered 12/6, 2012 at 9:29 Comment(2)
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!Westmorland
This might not work as you hope in unit tests or web projects running local IIS. The CodeBase answer does.Faltboat
G
18

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.

Greeneyed answered 7/1, 2021 at 20:58 Comment(0)
B
8

Safest way:

string temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Boulogne answered 12/6, 2012 at 9:22 Comment(2)
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.Nereen
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)Faltboat
I
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");
Iodize answered 19/4, 2019 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.