Skip to main content

How to Get the Folder Your App Runs From with AppContext.BaseDirectory in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

AppContext.BaseDirectory is the folder that holds your built app, wherever the app was started from. A file next to the app is found with Path.Combine(AppContext.BaseDirectory, "<name>"), while its bare name only looks in the current folder.

Two folders matter when an app opens a file by name. The app folder is where the build put the app. The .NET API reference on AppContext.BaseDirectory: Gets the file path of the base directory of the application. The current folder is the one the app works in, and a bare file name starts from there. The File.Exists reference: Relative path information is interpreted as relative to the current working directory.

When you start an app with dotnet run, these are two different folders. Mixing them up is why a file that's right next to the app can come back as missing.

Syntax​

Syntax
string <appFolder> = AppContext.BaseDirectory;
string <filePath> = Path.Combine(AppContext.BaseDirectory, "<name>");
PartWhat it is
AppContext.BaseDirectoryA string with the full path of the app folder, the folder that holds the built app
<appFolder>A variable that keeps it
Path.Combine(AppContext.BaseDirectory, "<name>")Joins the app folder and a file name into one path
<name>The name of a file in the app folder, such as the app's own AppBaseDirectory.dll
<filePath>The file's full path, which doesn't depend on the current folder

The Example​

A console app called AppBaseDirectory prints its app folder and its current folder. Then it looks for its own file, AppBaseDirectory.dll, twice: once through the app folder, and once by its bare name, which starts from the current folder.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n AppBaseDirectory
cd AppBaseDirectory
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/AppBaseDirectory/AppBaseDirectory.csproj:
Determining projects to restore...
Restored /Users/you/source/AppBaseDirectory/AppBaseDirectory.csproj (in 45 ms).
Restore succeeded.

dotnet new console -n AppBaseDirectory creates a folder named AppBaseDirectory with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or AppBaseDirectory.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First, the app folder:

Program.cs (the app folder)
// The folder that holds the built app, such as AppBaseDirectory.dll.
Console.WriteLine($"App folder: {AppContext.BaseDirectory}");

AppContext.BaseDirectory is a static property, so it's read from the AppContext class itself, with no object.

Then the current folder:

Program.cs (the current folder)
// The current working folder. Relative paths start from here.
Console.WriteLine($"Current folder: {Directory.GetCurrentDirectory()}");

Directory.GetCurrentDirectory() returns the current folder. The reference: Gets the current working directory of the application.

Then the same file, looked up both ways:

Program.cs (find the app's own file)
// The app's own file is found through the app folder, not through a relative path.
string dll = "AppBaseDirectory.dll";
Console.WriteLine($"{dll} in the app folder: {File.Exists(Path.Combine(AppContext.BaseDirectory, dll))}");
Console.WriteLine($"{dll} in the current folder: {File.Exists(dll)}");

Path.Combine joins the app folder and the file name into one path, and File.Exists returns true when there's a file at that path. The second File.Exists gets only the name, so it looks in the current folder.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
App folder: /Users/you/source/AppBaseDirectory/bin/Debug/net10.0/
Current folder: /Users/you/source/AppBaseDirectory
AppBaseDirectory.dll in the app folder: True
AppBaseDirectory.dll in the current folder: False

macOS-style terminal window titled Terminal — AppBaseDirectory showing dotnet run printing App folder: /Users/you/source/AppBaseDirectory/bin/Debug/net10.0/, Current folder: /Users/you/source/AppBaseDirectory, AppBaseDirectory.dll in the app folder: True and AppBaseDirectory.dll in the current folder: False

The app folder is bin/Debug/net10.0/ inside the project. The dotnet run reference explains it: Output files are written into the default location, which is bin/<configuration>/<target>. The current folder is the project folder, where dotnet run was typed. AppBaseDirectory.dll was found through the app folder, True, but not by its bare name, False, because the bare name was looked up in the project folder.

The Complete File​

macOS-style code window titled Program.cs — AppBaseDirectory showing lines that print AppContext.BaseDirectory and Directory.GetCurrentDirectory, and two File.Exists checks for AppBaseDirectory.dll, one through Path.Combine with AppContext.BaseDirectory and one by its bare name

Program.cs
// The folder that holds the built app, such as AppBaseDirectory.dll.
Console.WriteLine($"App folder: {AppContext.BaseDirectory}");

// The current working folder. Relative paths start from here.
Console.WriteLine($"Current folder: {Directory.GetCurrentDirectory()}");

// The app's own file is found through the app folder, not through a relative path.
string dll = "AppBaseDirectory.dll";
Console.WriteLine($"{dll} in the app folder: {File.Exists(Path.Combine(AppContext.BaseDirectory, dll))}");
Console.WriteLine($"{dll} in the current folder: {File.Exists(dll)}");

Run It from Another Folder​

The app folder stays the same wherever the app is started from, and the current folder doesn't. Go up one folder, and run the same project from there:

Terminal
cd ..
dotnet run --project AppBaseDirectory

cd .. moves up to the folder that holds the project. --project AppBaseDirectory tells dotnet run which project to run. The reference: Specifies the path of the project file to run (folder name or full path). If not specified, it defaults to the current directory. The app printed:

Output
App folder: /Users/you/source/AppBaseDirectory/bin/Debug/net10.0/
Current folder: /Users/you/source
AppBaseDirectory.dll in the app folder: True
AppBaseDirectory.dll in the current folder: False

The app folder is the same as before. The current folder is now /Users/you/source, where the command was typed, and the app still found its own file through the app folder.

Common Mistakes​

MistakeWhat happensDo this instead
Joining a folder and a file name with +, as in Directory.GetCurrentDirectory() + "settings.txt"A check printed /Users/you/source/AppBaseDirectorysettings.txt. The current folder doesn't end with a /, so the name was stuck onto the folder's nameUse Path.Combine. In the same check, Path.Combine(folder, "settings.txt") printed /Users/you/source/AppBaseDirectory/settings.txt
Opening a file the app needs by its bare nameThe name is looked up in the current folder, which depends on where the app was started. The run printed AppBaseDirectory.dll in the current folder: FalseBuild the path from AppContext.BaseDirectory
Expecting AppContext.BaseDirectory to be the project folderThe run printed /Users/you/source/AppBaseDirectory/bin/Debug/net10.0/, the folder the build writes the app toA file that the app looks for there has to be in that folder, next to the app's .dll

FAQ​

Is it the same as AppDomain.CurrentDomain.BaseDirectory?​

Yes. Older code often uses AppDomain.CurrentDomain.BaseDirectory. The AppContext.BaseDirectory reference: Its value corresponds to the AppDomain.BaseDirectory property of the current application domain. A check that compared the two printed The same folder: True.

What does it return for an app published as a single file?​

The .NET publishing overview: When you publish your app as a single-file deployment, all application-dependent files are bundled into a single binary. For that case, the AppContext.BaseDirectory reference says: In .NET 5 and later versions, for bundled assemblies, the value returned is the containing directory of the host executable. And the single-file guide recommends it: To access files next to the executable, use AppContext.BaseDirectory. The runs in this post used dotnet run only, where the app folder was bin/Debug/net10.0/.

Conclusion​

AppContext.BaseDirectory is the folder that holds the built app, and it doesn't change with the folder the app was started from. Build the path of any file that sits next to the app with Path.Combine(AppContext.BaseDirectory, "<name>"). Keep bare names and relative paths for files in the current folder, which Directory.GetCurrentDirectory() shows.

Additional Resources​