{"record":{"id":"a8fa7c5a0fcb0fc3","repo":"dotnet/wpf","slug":"stg-e-filenotfound","errorCode":"STG_E_FILENOTFOUND","errorMessage":"SR.ContainerNotFound","messagePattern":"SR\\.ContainerNotFound","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":352,"sourceCode":"            // STGM_CREATE not set, call StgOpenStorageEx.\n            returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageEx(\n                path,\n                grfMode,\n                stgFormatDocFile,\n                0,\n                IntPtr.Zero,\n                IntPtr.Zero,\n                ref IID_IStorage,\n                out newRootStorage );\n        }\n\n        switch( returnValue )\n        {\n            case SafeNativeCompoundFileConstants.S_OK:\n                return StorageRoot.CreateOnIStorage( \n                    newRootStorage );\n            case SafeNativeCompoundFileConstants.STG_E_FILENOTFOUND:\n                throw new FileNotFoundException( \n                    SR.ContainerNotFound);\n            case SafeNativeCompoundFileConstants.STG_E_INVALIDFLAG:\n                throw new ArgumentException( \n                    SR.StorageFlagsUnsupported,\n                    new COMException(\n                        SR.CFAPIFailure, \n                        returnValue));\n            default:\n                throw new IOException(\n                    SR.ContainerCanNotOpen,\n                    new COMException(\n                        SR.CFAPIFailure, \n                        returnValue));\n        }\n    }\n\n    /// <summary>\n    /// Clean up this container storage instance","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L334-L370","documentation":"When opening an existing compound file container, the native StgOpenStorageEx call returned STG_E_FILENOTFOUND, which StorageRoot.Open translates to FileNotFoundException with message ContainerNotFound. The path given does not contain an openable container file.","triggerScenarios":"Calling StorageRoot.Open / Package.Open with FileMode.Open on a path where no file exists, or the file was deleted/moved between the check and the open.","commonSituations":"Typos in the package path; relative vs current-directory mismatch; opening an .xlsx/.docx that a prior step failed to write; deleted temp files.","solutions":["Check File.Exists(path) before opening and correct the path.","Use FileMode.OpenOrCreate or Create when the file may legitimately not exist yet.","Handle FileNotFoundException and surface a user-friendly message.","Ensure the working directory / absolute path is correct."],"exampleFix":"// before\nvar pkg = Package.Open(path, FileMode.Open, FileAccess.Read); // throws if missing\n// after\nif (!File.Exists(path))\n    throw new FileNotFoundException(\"Package not found\", path);\nvar pkg = Package.Open(path, FileMode.Open, FileAccess.Read);","handlingStrategy":"try-catch","validationCode":"if (!File.Exists(path))\n    throw new FileNotFoundException(\"Package container not found\", path);","typeGuard":"bool ContainerExists(string path) => File.Exists(path);","tryCatchPattern":"try { pkg = Package.Open(path, FileMode.Open, FileAccess.Read); }\ncatch (FileNotFoundException ex) { /* prompt user or create package */ }","preventionTips":["Check File.Exists and normalize to absolute paths before opening.","Use OpenOrCreate when creating on demand.","Verify output steps that produce the package completed successfully."],"tags":["io","file-not-found","compound-file"],"backgroundTag":"file-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}