{"record":{"id":"49f8cacd53a17724","repo":"microsoft/semantic-kernel","slug":"file-filename-not-found","errorCode":null,"errorMessage":"File '{fileName}' not found.","messagePattern":"File '(.+?)' not found\\.","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/Demos/OpenAIRealtime/Program.cs","lineNumber":393,"sourceCode":"            }\n        }\n    }\n\n    /// <summary>Helper method to get a file path.</summary>\n    private static string FindFile(string fileName)\n    {\n        for (string currentDirectory = Directory.GetCurrentDirectory();\n             currentDirectory != null && currentDirectory != Path.GetPathRoot(currentDirectory);\n             currentDirectory = Directory.GetParent(currentDirectory)?.FullName!)\n        {\n            string filePath = Path.Combine(currentDirectory, fileName);\n            if (File.Exists(filePath))\n            {\n                return filePath;\n            }\n        }\n\n        throw new FileNotFoundException($\"File '{fileName}' not found.\");\n    }\n\n    /// <summary>\n    /// Helper method to get an instance of <see cref=\"RealtimeClient\"/> based on provided\n    /// OpenAI or Azure OpenAI configuration.\n    /// </summary>\n    private static RealtimeClient GetRealtimeConversationClient()\n    {\n        var config = new ConfigurationBuilder()\n            .AddUserSecrets<Program>()\n            .AddEnvironmentVariables()\n            .Build();\n\n        var openAIOptions = config.GetSection(OpenAIOptions.SectionName).Get<OpenAIOptions>()!;\n        var azureOpenAIOptions = config.GetSection(AzureOpenAIOptions.SectionName).Get<AzureOpenAIOptions>()!;\n\n        if (openAIOptions is not null && openAIOptions.IsValid)\n        {","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/OpenAIRealtime/Program.cs#L375-L411","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nthrow new FileNotFoundException($\"File '{fileName}' not found.\");\n\n// after — fall back to the application base directory\nvar basePath = AppContext.BaseDirectory;\nvar resolved = Path.Combine(basePath, fileName);\nif (!File.Exists(resolved))\n    throw new FileNotFoundException($\"File '{fileName}' not found in {basePath} or any parent directory.\");\nreturn resolved;","handlingStrategy":"validation","validationCode":"// Check common locations before the upward walk\nvar candidates = new[] { AppContext.BaseDirectory, Directory.GetCurrentDirectory(), AppDomain.CurrentDomain.BaseDirectory };\nvar found = candidates.Select(d => Path.Combine(d, fileName)).FirstOrDefault(File.Exists);\nif (found is null)\n    found = SearchUpward(fileName); // your existing walk","typeGuard":null,"tryCatchPattern":"try { return SearchUpwardForFile(fileName); } catch (FileNotFoundException) { /* log and use a default or embedded resource */ return EmbeddedResourcePath; }","preventionTips":["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."],"tags":["file-not-found","filesystem","path-search","samples","resources"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}