lepoco/wpfui · critical · DirectoryNotFoundException
Unable to find the base directory of the application.
Error message
Unable to find the base directory of the application.
What it means
Thrown during generic Host construction when Path.GetDirectoryName(AppContext.BaseDirectory) returns null. The host needs a base path to locate appsettings.json and other configuration files, so a null base directory prevents ConfigureAppConfiguration from establishing a config root. In practice AppContext.BaseDirectory is virtually always set for a running .NET app, so this is a defensive guard for an exotic hosting/published-layout edge case rather than a routine failure.
Source
Thrown at samples/Wpf.Ui.Demo.Mvvm/App.xaml.cs:31
namespace Wpf.Ui.Demo.Mvvm;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
// The.NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
private static readonly IHost _host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(c =>
{
var basePath =
Path.GetDirectoryName(AppContext.BaseDirectory)
?? throw new DirectoryNotFoundException(
"Unable to find the base directory of the application."
);
_ = c.SetBasePath(basePath);
})
.ConfigureServices(
(context, services) =>
{
_ = services.AddNavigationViewPageProvider();
// App Host
_ = services.AddHostedService<ApplicationHostService>();
// Theme manipulation
_ = services.AddSingleton<IThemeService, ThemeService>();
// TaskBar manipulation
_ = services.AddSingleton<ITaskBarService, TaskBarService>();
View on GitHub (pinned to ffebacd610)
Solutions
- Verify AppContext.BaseDirectory is populated at runtime (log it on startup) and confirm the executable is launched normally rather than through a path-stripping wrapper.
- If publishing single-file, ensure the publish profile does not relocate the base directory; set the working/base path explicitly via Dotnet.Bundle or SetBasePath(Directory.GetCurrentDirectory()) as a fallback.
- Pass an explicit known path into ConfigureAppConfiguration (e.g. AppContext.BaseDirectory, then Directory.GetCurrentDirectory()) so SetBasePath always receives a non-null value.
Example fix
// before
var basePath =
Path.GetDirectoryName(AppContext.BaseDirectory)
?? throw new DirectoryNotFoundException(
"Unable to find the base directory of the application.");
_ = c.SetBasePath(basePath);
// after
var basePath =
Path.GetDirectoryName(AppContext.BaseDirectory)
?? Directory.GetCurrentDirectory();
_ = c.SetBasePath(basePath); Defensive patterns
Strategy: validation
Validate before calling
// Validate before the host builds
string? baseDir = Path.GetDirectoryName(AppContext.BaseDirectory);
if (string.IsNullOrEmpty(baseDir))
{
baseDir = Directory.GetCurrentDirectory();
}
// then use baseDir in ConfigureAppConfiguration Try / catch
try { Host.CreateDefaultBuilder().ConfigureAppConfiguration(c => c.SetBasePath(baseDir))... } catch (DirectoryNotFoundException) { /* fall back to current directory and log */ } Prevention
- Always provide a fallback base path (e.g. Directory.GetCurrentDirectory()) so SetBasePath never receives null.
- Log AppContext.BaseDirectory at startup to detect environment stripping early.
- Validate publish profiles for single-file/self-contained scenarios that can alter the base path.
When it happens
Trigger: Host.CreateDefaultBuilder().ConfigureAppConfiguration(...) runs at static field initialization of the App class, and Path.GetDirectoryName returns null when AppContext.BaseDirectory is empty or a root-only path (e.g. exactly "\" on Windows where GetDirectoryName yields null).
Common situations: Misconfigured publish profile (single-file or self-contained extraction quirks), running under a host/process that zeroes the app base path, or test harnesses that load the assembly without a normal app domain base directory. Also reproducible if the sample is launched via a shim that strips the path.
Related errors
- DialogHost was not set
- Only one ContentDialogHost instance is allowed per Window.
- Cannot apply backdrop effect if ExtendsContentIntoTitleBar i
- The {nameof(viewItem)}.{nameof(viewItem.TargetPageType)} pro
- {nameof(_serviceProvider)}.{nameof(_serviceProvider.GetServi
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/f1a0824a8ba7304d.
Report an issue: GitHub.