Member-only story
Exploring Different Methods to Fetch the Application’s Path
In a .NET console application, getting the path to the application’s directory is a common task for various purposes like file handling, logging, and configuration management. Thankfully, .NET offers several techniques to achieve this.
The article showcases various methods to retrieve the application’s path in a .NET console application.
1. Utilizing AppDomain.CurrentDomain.BaseDirectory
A straightforward and widely used method is to employ the AppDomain.CurrentDomain.BaseDirectory
property. This property returns the base directory where the application is currently running.
string appPath = AppDomain.CurrentDomain.BaseDirectory;
2. Employing Assembly.GetExecutingAssembly().Location
Another approach is to use the Assembly.GetExecutingAssembly().Location
property. This provides the file path of the assembly (executable) containing the currently executing code.
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);