dotnet/wpf · error · FileNotFoundException
STG_E_FILENOTFOUND
STG_E_FILENOTFOUND
Error message
SR.ContainerNotFound
What it means
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.
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.
Example fix
// before
var pkg = Package.Open(path, FileMode.Open, FileAccess.Read); // throws if missing
// after
if (!File.Exists(path))
throw new FileNotFoundException("Package not found", path);
var pkg = Package.Open(path, FileMode.Open, FileAccess.Read); Defensive patterns
Strategy: try-catch
Validate before calling
if (!File.Exists(path))
throw new FileNotFoundException("Package container not found", path); Type guard
bool ContainerExists(string path) => File.Exists(path);
Try / catch
try { pkg = Package.Open(path, FileMode.Open, FileAccess.Read); }
catch (FileNotFoundException ex) { /* prompt user or create package */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Typos in the package path; relative vs current-directory mismatch; opening an .xlsx/.docx that a prior step failed to write; deleted temp files.
Understand the failure class
Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.
Related errors
- Cannot open data stream.
- No file exists at
- SR.CanNotCreateContainerOnReadOnlyStream
- SR.CanNotCreateStorageRootOnNonReadableStream
- SR.CanNotDelete
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a8fa7c5a0fcb0fc3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs:352
// STGM_CREATE not set, call StgOpenStorageEx.
returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageEx(
path,
grfMode,
stgFormatDocFile,
0,
IntPtr.Zero,
IntPtr.Zero,
ref IID_IStorage,
out newRootStorage );
}
switch( returnValue )
{
case SafeNativeCompoundFileConstants.S_OK:
return StorageRoot.CreateOnIStorage(
newRootStorage );
case SafeNativeCompoundFileConstants.STG_E_FILENOTFOUND:
throw new FileNotFoundException(
SR.ContainerNotFound);
case SafeNativeCompoundFileConstants.STG_E_INVALIDFLAG:
throw new ArgumentException(
SR.StorageFlagsUnsupported,
new COMException(
SR.CFAPIFailure,
returnValue));
default:
throw new IOException(
SR.ContainerCanNotOpen,
new COMException(
SR.CFAPIFailure,
returnValue));
}
}
/// <summary>
/// Clean up this container storage instanceView on GitHub (pinned to 81131a70a4)