dotnet/wpf · error · FileFormatException
Document does not contain a package.
Error message
Document does not contain a package.
What it means
InitForOpen (used by EncryptedPackageEnvelope.Open) validates that the compound file contains the expected package stream. If the required StreamInfo does not exist, it throws FileFormatException indicating the document has no embedded package.
Solutions
- Verify the file is genuinely a rights-managed encrypted package (created via EncryptedPackageEnvelope.Create*) before calling Open
- Check for the package stream beforehand using StorageInfo/OLE compound-file inspection, or handle FileFormatException and fall back to a plain OpenPackage path
- Recover the original file (re-download, restore from backup) if it was corrupted or truncated
Example fix
// before
var env = EncryptedPackageEnvelope.Open(path); // FileFormatException if no package stream
// after
try { var env = EncryptedPackageEnvelope.Open(path); }
catch (FileFormatException) { /* treat as non-packaged document; use normal open path */ } Defensive patterns
Strategy: try-catch
Validate before calling
var root = StorageInfo.Open(path);
var si = new StreamInfo(root, "\u0006DataSpaces/TransformedPackage/Transformed" ); // confirm package stream exists before Open
if (!si.Exists) throw new FileNotFoundException("Not a rights-managed package.", path); Type guard
bool LooksLikeEncryptedPackage(string path) => File.Exists(path) && new FileInfo(path).Length > 0; // plus OLE header check
Try / catch
try { var env = EncryptedPackageEnvelope.Open(path); }
catch (FileFormatException) { /* document has no package: use normal package open path */ } Prevention
- Only call EncryptedPackageEnvelope.Open on files produced by RM-protecting applications
- Handle FileFormatException with a fallback to Package.Open
- Restore from source if the file may have been truncated in transit
When it happens
Trigger: Calling EncryptedPackageEnvelope.Open on a compound file whose internal storage lacks the package stream — e.g. an empty or non-XPS/RM file, a file saved by a different tool, or a file truncated/corrupted during transfer.
Common situations: Opening an ordinary OLE compound document (Word 97 .doc, Excel .xls) expecting it to be a rights-managed package; opening a file where an earlier process failed mid-write; wrong file passed to Open.
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
- Document does not contain any rights management-protected…
- SR.Format(SR.PublishLicenseStreamHeaderTooLong, headerLen…
- SR.PublishLicenseStreamCorrupt
- Document contains multiple Rights Management Encryption…
- Signature structures are corrupted in this package.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/97c86c370e825579.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/EncryptedPackage.cs:879
// Prepare the transform object for use.
//
rmet.SavePublishLicense(publishLicense);
rmet.CryptoProvider = cryptoProvider;
//
// The transform object is now ready for use. When the data space manager
// queries the transform's IsReady property, it will return true. So there
// is no need to sign up for the TransformInitializationEvent.
//
}
private void
InitForOpen()
{
StreamInfo siPackage = new StreamInfo(_root, PackageStreamName);
if (!siPackage.InternalExists())
{
throw new FileFormatException(SR.PackageNotFound);
}
//If the StreamInfo exists we go on to check if correct transform has been
//applied to the Stream
DataSpaceManager dsm = _root.GetDataSpaceManager();
List<IDataTransform> transforms = dsm.GetTransformsForStreamInfo(siPackage);
RightsManagementEncryptionTransform rmet = null;
foreach (IDataTransform dataTransform in transforms)
{
if (dataTransform.TransformIdentifier is string id &&
string.Equals(id, RightsManagementEncryptionTransform.ClassTransformIdentifier, StringComparison.OrdinalIgnoreCase))
{
// Do not allow more than one RM Transform
if (rmet != null)View on GitHub (pinned to 81131a70a4)