How can I get the application's path in a .NET console application?
System.Reflection.Assembly.GetExecutingAssembly()
.Location
1
Combine that with System.IO.Path.GetDirectoryName
if all you want is the directory.
1As per Mr.Mindor's comment:
System.Reflection.Assembly.GetExecutingAssembly().Location
returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory.System.Reflection.Assembly.GetExecutingAssembly().CodeBase
will return the 'permanent' path of the assembly.
Best way to get application folder path
AppDomain.CurrentDomain.BaseDirectory
is probably the most useful for accessing files whose location is relative to the application install directory.
In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client application, it will be the directory containing the main executable.
In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your application, not, say, the path to the Excel executable.
The others may return different directories depending on your environment - for example see @Vimvq1987's answer.
CodeBase
is the place where a file was found and can be a URL beginning with http://. In which case Location
will probably be the assembly download cache. CodeBase is not guaranteed to be set for assemblies in the GAC.
UPDATE
These days (.NET Core, .NET Standard 1.3+ or .NET Framework 4.6+) it's better to use AppContext.BaseDirectory
rather than AppDomain.CurrentDomain.BaseDirectory
. Both are equivalent, but multiple AppDomains are no longer supported.
how to get the path in a .NET console application
Directory.GetCurrentDirectory method is what you are looking for.
In .NET console app, how to get the path of the command prompt where the user typed the command?
Directory.GetCurrentDirectory() will do what you need (get the current working directory).
How to get execution directory of console application
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.
How to get the path of the directory where the .exe file for a .Net Core console application is located?
I have found the answer thanks to this issue: https://github.com/dotnet/runtime/issues/13051
You have to use: Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
Get application directory using C# Console Application?
If you still want to use Application.ExecutablePath in console application you need to:
- Add a reference to System.Windows.Forms namespace
Add System.Windows.Forms to your usings section
using System;
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
Console.WriteLine(appDirectory);
}
}
}
Also you can use Directory.GetCurrentDirectory()
instead of Path.GetDirectoryName(Application.ExecutablePath)
and thus you won't need a reference to System.Windows.Forms.
If you'd like not to include neither System.IO
nor System.Windows.Forms
namespaces then you should follow Reimeus's answer.
Get my application’s current path
to get the execution folder:
System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
to get the current directory:
Directory.GetCurrentDirectory()
they aren't necessarily the same thing, so be careful
Related Topics
Easiest Way to Read from and Write to Files
What's a "Static Method" in C#
How to Implement Ixmlserializable
Filesystemwatcher VS Polling to Watch For File Changes
How to Avoid Dependency Injection Constructor Madness
What Is a Nullreferenceexception, and How to Fix It
What Is an Indexoutofrangeexception/Argumentouto
frangeexception and How to Fix It
Random Number Generator Only Generating One Random Number
How to Update the Gui from Another Thread
How to Post Messages to an Sta Thread Running a Message Pump
Dependency Inject (Di) "Friendly" Library
How to Create a New Object Instance from a Type
Difference Between Property and Field in C# 3.0+
Best Way to Randomize an Array With .Net
Parse String to Datetime in C#
How to Make Realistic N-Body Solar System Simulation in Matter of Size and Mass