dotnet/wpf · error · ArgumentException
Stream name cannot be
Error message
Stream name cannot be '{0}'. What it means
StorageInfo.CreateStream rejects the reserved stream name used by EncryptedPackageEnvelope (the encrypted-package marker in Rights-Managed/protected compound documents). Because the name collides with a special package stream, the library throws ArgumentException with the 'Stream name cannot be ...' message.
Solutions
- Choose a different stream name that does not match the reserved EncryptedPackageEnvelope.PackageStreamName
- Compare the intended name against EncryptedPackageEnvelope.PackageStreamName (case-insensitive) before calling CreateStream
- If writing an actual encrypted package, use EncryptedPackageEnvelope APIs instead of raw CreateStream
Example fix
// before
storage.CreateStream(userSuppliedName, CompressionOption.NotCompressed);
// after
if (string.Equals(userSuppliedName, EncryptedPackageEnvelope.PackageStreamName, StringComparison.OrdinalIgnoreCase))
userSuppliedName = "Content" + userSuppliedName;
storage.CreateStream(userSuppliedName, CompressionOption.NotCompressed); Defensive patterns
Strategy: validation
Validate before calling
bool reserved = string.Equals(name, EncryptedPackageEnvelope.PackageStreamName, StringComparison.OrdinalIgnoreCase); if (!reserved) storage.CreateStream(name, CompressionOption.NotCompressed);
Type guard
static bool IsReservedStreamName(string name) => string.Equals(name, EncryptedPackageEnvelope.PackageStreamName, StringComparison.OrdinalIgnoreCase);
Try / catch
try { storage.CreateStream(name, CompressionOption.NotCompressed); }
catch (ArgumentException ex) when (ex.Message.Contains("cannot be")) { /* pick another name */ } Prevention
- Filter user-derived stream names against EncryptedPackageEnvelope.PackageStreamName
- Never allow raw user input as compound-file stream names without validation
When it happens
Trigger: Calling storage.CreateStream with a name equal (case-insensitively) to EncryptedPackageEnvelope.PackageStreamName, e.g. creating a user stream named like the encrypted-package marker.
Common situations: Applications writing custom parts into XPS/OPC compound files that accidentally pick the reserved encrypted-package name; code that derives stream names from user input without filtering reserved names.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.BamlWriterBadStream
- SR.CanNotCreateStorageRootOnNonReadableStream
- SR.StreamAlreadyExist
- SR.StreamNotExist
- SR.UnableToCreateOnStream
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/2ef9aee6dc52a12d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs:268
// public Methods
/// <summary>
/// Creates "this" stream
/// </summary>
/// <param name="name">Name of stream</param>
/// <param name="compressionOption">CompressionOptiont</param>
/// <param name="encryptionOption">EncryptionOption</param>
/// <returns>Reference to new stream</returns>
public StreamInfo CreateStream( string name, CompressionOption compressionOption, EncryptionOption encryptionOption )
{
CheckDisposedStatus();
//check the arguments
ArgumentNullException.ThrowIfNull(name);
// Stream names: we preserve casing, but do case-insensitive comparison (Native CompoundFile API behavior)
if (string.Equals(name, EncryptedPackageEnvelope.PackageStreamName, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(SR.Format(SR.StreamNameNotValid,name));
//create a new streaminfo object
StreamInfo streamInfo = new StreamInfo(this, name, compressionOption, encryptionOption);
if (streamInfo.InternalExists())
{
throw new IOException(SR.StreamAlreadyExist);
}
//Define the compression and encryption options in the dataspacemanager
DataSpaceManager manager = Root.GetDataSpaceManager();
string dataSpaceLabel = null;
if (manager != null)
{
//case : Compression option is set. Stream need to be compressed. Define compression transform.
//At this time, we only treat CompressionOption - Normal and None. The rest are treated as Normal
if (compressionOption != CompressionOption.NotCompressed)
{View on GitHub (pinned to 81131a70a4)