microsoft/semantic-kernel · error · FileNotFoundException
File '{fileName}' not found.
Error message
File '{fileName}' not found. What it means
Thrown by a helper that walks the directory tree upward from the current working directory searching for a named file. It checks each ancestor directory from CWD to the filesystem root; if the file is not found in any of them, it throws FileNotFoundException. The error is used to locate resource/config files shipped relative to the repo root.
Source
Thrown at dotnet/samples/Demos/OpenAIRealtime/Program.cs:393
}
}
}
/// <summary>Helper method to get a file path.</summary>
private static string FindFile(string fileName)
{
for (string currentDirectory = Directory.GetCurrentDirectory();
currentDirectory != null && currentDirectory != Path.GetPathRoot(currentDirectory);
currentDirectory = Directory.GetParent(currentDirectory)?.FullName!)
{
string filePath = Path.Combine(currentDirectory, fileName);
if (File.Exists(filePath))
{
return filePath;
}
}
throw new FileNotFoundException($"File '{fileName}' not found.");
}
/// <summary>
/// Helper method to get an instance of <see cref="RealtimeClient"/> based on provided
/// OpenAI or Azure OpenAI configuration.
/// </summary>
private static RealtimeClient GetRealtimeConversationClient()
{
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
.Build();
var openAIOptions = config.GetSection(OpenAIOptions.SectionName).Get<OpenAIOptions>()!;
var azureOpenAIOptions = config.GetSection(AzureOpenAIOptions.SectionName).Get<AzureOpenAIOptions>()!;
if (openAIOptions is not null && openAIOptions.IsValid)
{View on GitHub (pinned to c028a0c7dc)
Solutions
- Verify the file actually exists somewhere under your repo or project tree, then run the app from a subdirectory of that tree.
- Place the file in the application's base/output directory so the downward-from-root walk finds it, or copy it to the working directory.
- Pass the full absolute path to the file directly instead of relying on the upward directory walk.
- Embed the file as a project resource or content file with CopyToOutputDirectory so it ships with the build output.
Example fix
// before
throw new FileNotFoundException($"File '{fileName}' not found.");
// after — fall back to the application base directory
var basePath = AppContext.BaseDirectory;
var resolved = Path.Combine(basePath, fileName);
if (!File.Exists(resolved))
throw new FileNotFoundException($"File '{fileName}' not found in {basePath} or any parent directory.");
return resolved; Defensive patterns
Strategy: validation
Validate before calling
// Check common locations before the upward walk
var candidates = new[] { AppContext.BaseDirectory, Directory.GetCurrentDirectory(), AppDomain.CurrentDomain.BaseDirectory };
var found = candidates.Select(d => Path.Combine(d, fileName)).FirstOrDefault(File.Exists);
if (found is null)
found = SearchUpward(fileName); // your existing walk Try / catch
try { return SearchUpwardForFile(fileName); } catch (FileNotFoundException) { /* log and use a default or embedded resource */ return EmbeddedResourcePath; } Prevention
- Embed critical files as resources or set CopyToOutputDirectory in .csproj.
- Accept an override path via configuration or CLI argument.
- Log the searched directories in the error message to speed up diagnosis.
When it happens
Trigger: Calling the file-search helper (e.g., GetFilePath) with a fileName that doesn't exist in the current directory or any ancestor directory — typical when the app is deployed or run from a directory that isn't nested under the repo, or when the target file was never copied to the output.
Common situations: Running the OpenAIRealtime sample from a published/packaged location where the config or assets file isn't present; running from /tmp or a container where the parent chain doesn't contain the file; the file name was renamed or the path convention changed between sample versions.
Related errors
- Plugins directory not found. The app needs the plugins from
- File {path} not found in repository.
- File {file_name} not found in repository.
- Configuration not found, please setup the notebooks first us
- Invalid choice
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/49f8cacd53a17724.
Report an issue: GitHub.