microsoft/semantic-kernel · error · DirectoryNotFoundException
The directory '{directoryPath}' does not exist.
Error message
The directory '{directoryPath}' does not exist. What it means
A DirectoryNotFoundException thrown when the absolute path provided to GenerateMermaidImageAsync points into a directory that does not exist on disk. The renderer refuses to write into a missing directory rather than silently creating it for absolute paths.
Source
Thrown at dotnet/samples/GettingStartedWithProcesses/Utilities/MermaidRenderer.cs:41
if (!filenameOrPath.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("The filename must have a .png extension.", nameof(filenameOrPath));
}
string outputFilePath;
// Check if the user provided an absolute path
if (Path.IsPathRooted(filenameOrPath))
{
// Use the provided absolute path
outputFilePath = filenameOrPath;
// Ensure the directory exists
string directoryPath = Path.GetDirectoryName(outputFilePath)
?? throw new InvalidOperationException("Could not determine the directory path.");
if (!Directory.Exists(directoryPath))
{
throw new DirectoryNotFoundException($"The directory '{directoryPath}' does not exist.");
}
}
else
{
// Use the assembly's directory for relative paths
string? assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (assemblyPath == null)
{
throw new InvalidOperationException("Could not determine the assembly path.");
}
string outputPath = Path.Combine(assemblyPath, "output");
Directory.CreateDirectory(outputPath); // Ensure output directory exists
outputFilePath = Path.Combine(outputPath, filenameOrPath);
}
// Download Chromium if it hasn't been installed yet
BrowserFetcher browserFetcher = new();View on GitHub (pinned to c028a0c7dc)
Solutions
- Create the target directory before calling: Directory.CreateDirectory(Path.GetDirectoryName(path)).
- Use a relative filename so the renderer's assembly 'output' folder branch (which auto-creates) is used.
- Point to a known-existing directory such as Path.GetTempPath().
Example fix
// before await MermaidRenderer.GenerateMermaidImageAsync(code, @"C:\out\diagram.png"); // C:\out missing // after string dir = @"C:\out"; Directory.CreateDirectory(dir); await MermaidRenderer.GenerateMermaidImageAsync(code, Path.Combine(dir, "diagram.png"));
Defensive patterns
Strategy: validation
Validate before calling
string dir = Path.GetDirectoryName(Path.GetFullPath(filename))!; Directory.CreateDirectory(dir); await MermaidRenderer.GenerateMermaidImageAsync(code, filename);
Type guard
bool DirectoryExistsFor(string p) { var d = Path.GetDirectoryName(Path.GetFullPath(p)); return d is not null && Directory.Exists(d); } Try / catch
try { await MermaidRenderer.GenerateMermaidImageAsync(code, filename); }
catch (DirectoryNotFoundException) { Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(filename))!); /* retry */ } Prevention
- Pre-create output directories before writing.
- Use Path.GetTempPath() as a known-good base.
- Prefer relative filenames to use the renderer's auto-created output folder.
When it happens
Trigger: Calling GenerateMermaidImageAsync with an absolute path whose parent directory has not been created (e.g. '/output/x.png' where /output is absent).
Common situations: Hard-coded output folder that was never created; running in an environment where the expected output directory differs; CI sandbox lacking the path.
Related errors
- The filename must have a .png extension.
- Could not determine the directory path.
- Could not determine the assembly path.
- An error occurred while accessing the file.
- Directory for path '{fullFilepath}' does not exist, could no
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/ccf32ab743718d4f.
Report an issue: GitHub.