dotnet/wpf · error · IOException

SR.ContainerCanNotOpen

Error message

SR.ContainerCanNotOpen

What it means

The native StgOpenStorageEx/StgCreateStorageEx call failed with an HRESULT not specifically mapped, so StorageRoot.Open throws IOException with ContainerCanNotOpen, wrapping a COMException(CFAPIFailure, hr). The container exists but could not be opened or created.

Solutions

  1. Inspect the inner COMException HResult (e.g. STG_E_FILEALREADYEXISTS, STG_E_NOTFILEBASEDSTORAGE, ACCESS_DENIED) and fix accordingly.
  2. Verify the file is a valid compound-file/OPC container and not corrupted; restore from backup.
  3. Ensure the file is not locked by another process and the user has the required permissions.
  4. Delete a zero-byte/corrupt file and recreate the package.
  5. Run as a user with sufficient rights or copy the file to a writable location.

Example fix

// before
var pkg = Package.Open(corruptedPath, FileMode.Open, FileAccess.ReadWrite);
// after
try
{
    var pkg = Package.Open(path, FileMode.Open, FileAccess.ReadWrite);
}
catch (IOException ex) when (ex.InnerException is COMException ce)
{
    // log ce.HResult, restore from backup or recreate the package
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!File.Exists(path) || new FileInfo(path).Length == 0)
    throw new IOException("Container missing or empty");

Try / catch

try { pkg = Package.Open(path, FileMode.Open, FileAccess.Read); }
catch (IOException ex) when (ex.InnerException is COMException ce)
{
    var hr = ce.HResult; // log and recover: restore backup, fix permissions, or recreate
}

Prevention

When it happens

Trigger: Any unmapped native failure during StorageRoot.Open: corrupted compound file, access denied, file locked with incompatible share, not a valid compound file (e.g. a plain ZIP renamed to .docx in some paths), disk errors.

Common situations: Opening a damaged/truncated package; opening a file that is not actually an OLE compound file; insufficient permissions; file held by another process with exclusive access.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f10c45665b9d7004. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs:361

                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 instance
    /// </summary>
    internal void Close()
    {
        if( null == rootIStorage )
            return; // Extraneous calls to Close() are ignored

        // Tell data space manager to flush all information as necessary
        dataSpaceManager?.Dispose();
        dataSpaceManager = null;

View on GitHub (pinned to 81131a70a4)