microsoft/semantic-kernel · error · InvalidOperationException
Could not determine the directory path.
Error message
Could not determine the directory path.
What it means
Thrown when Path.GetDirectoryName(outputFilePath) returns null for a rooted input. GetDirectoryName returns null only for paths like '/' or a bare root, meaning no directory component could be resolved. The renderer needs a concrete directory to validate/create.
Source
Thrown at dotnet/samples/GettingStartedWithProcesses/Utilities/MermaidRenderer.cs:38
public static async Task<string> GenerateMermaidImageAsync(string mermaidCode, string filenameOrPath)
{
// Ensure the filename has the correct .png extension
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);
}View on GitHub (pinned to c028a0c7dc)
Solutions
- Supply an absolute path that has a real parent directory, e.g. '/tmp/out/diagram.png'.
- Prepend a working directory when only a filename is needed: use a relative path so the assembly-directory branch is taken.
- Validate the path resolves to a non-root directory before calling.
Example fix
// before await MermaidRenderer.GenerateMermaidImageAsync(code, "/diagram.png"); // after string dir = Path.Combine(Path.GetTempPath(), "mermaid"); 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));
if (string.IsNullOrEmpty(dir) || dir == Path.GetPathRoot(dir))
throw new ArgumentException("Provide a path with a real parent directory.", nameof(filename)); Type guard
bool HasRealDirectory(string p) { var d = Path.GetDirectoryName(Path.GetFullPath(p)); return !string.IsNullOrEmpty(d) && d != Path.GetPathRoot(d); } Try / catch
try { await MermaidRenderer.GenerateMermaidImageAsync(code, filename); }
catch (InvalidOperationException ex) when (ex.Message.Contains("directory path")) { /* rebuild path with a concrete dir */ } Prevention
- Always combine a concrete base directory with the filename.
- Avoid root-only paths when constructing output locations.
- Use Path.GetFullPath to validate resolved paths early.
When it happens
Trigger: Calling GenerateMermaidImageAsync with an absolute path whose directory component is null, e.g. '/foo.png' on some platforms yields a directory, but a root-only path like '/' or a malformed path yields null.
Common situations: Passing a bare root path, an empty directory, or a path constructed incorrectly so GetDirectoryName returns null.
Related errors
- The filename must have a .png extension.
- The directory '{directoryPath}' does not exist.
- Could not determine the assembly path.
- An error occurred while accessing the file.
- File '{fileName}' not found.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/acc142fe18185d2c.
Report an issue: GitHub.