WPF
application directory
C#
.NET
programming

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

csharp
1using System;
2using System.IO;
3
4string appDirectory = AppContext.BaseDirectory;
5string configPath = Path.Combine(appDirectory, "settings.json");
6
7Console.WriteLine(appDirectory);
8Console.WriteLine(configPath);

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.

csharp
using System;

Console.WriteLine(Environment.CurrentDirectory);

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:

csharp
1using System;
2using System.IO;
3
4string? exePath = Environment.ProcessPath;
5string exeDirectory = Path.GetDirectoryName(exePath!)!;
6
7Console.WriteLine(exePath);
8Console.WriteLine(exeDirectory);

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:

csharp
1using System;
2using System.IO;
3using System.Windows;
4
5public partial class App : Application
6{
7    protected override void OnStartup(StartupEventArgs e)
8    {
9        base.OnStartup(e);
10
11        string configPath = Path.Combine(AppContext.BaseDirectory, "settings.json");
12
13        if (File.Exists(configPath))
14        {
15            string json = File.ReadAllText(configPath);
16            MessageBox.Show($"Loaded config with {json.Length} characters.");
17        }
18    }
19}

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:

csharp
1using System;
2
3string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
4Console.WriteLine(appData);

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms