Getting the application's directory from a WPF application
Codemia Knowledge Hub ·
Use AppContext.BaseDirectory or AppDomain.CurrentDomain.BaseDirectory. Both point to the base directory the runtime uses to resolve assemblies and nearby content files, which is the folder where the app is deployed. Do not use the current working directory: mixing the two up is a common source of bugs when loading configuration files, templates or other resources shipped next to the executable.
AppContext.BaseDirectory for deployed files
This is what you want when reading files that live next to the installed application.
Why Environment.CurrentDirectory is different
Environment.CurrentDirectory is the process working directory. It can change at run time and may not match the executable location at all: a file dialog, a launcher, a scheduled task or another library can point it elsewhere.
This prints a directory, but not necessarily the one where the WPF app is installed. For app-relative files, that is a bug waiting to happen.
Environment.ProcessPath for the executable itself
If you need the executable file path rather than the base directory, newer .NET versions offer Environment.ProcessPath:
The base directory and the executable path are related but not identical, so be clear about which one you need.
Why Assembly.Location is a weaker choice
Many older examples use Assembly.GetExecutingAssembly().Location or Assembly.GetEntryAssembly()?.Location. These can work, but publishing models such as single-file deployment make assembly-location semantics less straightforward. For "where should I read files from?" logic, AppContext.BaseDirectory tracks the deployed application base more directly.
Loading config at startup
A typical WPF app loads configuration from the application folder in OnStartup:
It does not depend on whichever directory the process happened to start in.
Writable data belongs in app-data folders
The application directory suits read-only files deployed with the app. User data, logs, caches and writable settings usually belong in a user-specific app-data folder, because writing next to the executable can cause permission problems or awkward deployment behavior:
Related reading
- Getting the .NET Framework directory path
- Getting the path of the home directory in C?
- Getting the PublicKeyToken of .Net assemblies
- .gitignore and Visual Studio project Ignore bin/Debug directory but not bin/Release directory
- .gitignore for Visual Studio Projects and Solutions
- Global keyboard capture in C application
- Global setting for AsNoTracking?
- GO statements blowing up sql execution in .NET

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.