dotnet/wpf · error · NotSupportedException
SR.SetLengthNotSupported
Error message
SR.SetLengthNotSupported
What it means
CFStream.SetLength throws NotSupportedException with SR.SetLengthNotSupported when the stream was opened without write access. Changing a compound-file stream's size requires write mode; on a read-only stream the operation is unsupported, matching Stream contract. The check happens after the disposed check and before length validation.
Solutions
- Open the compound file with write access (FileAccess.ReadWrite) so CanWrite is true
- Guard with if (stream.CanWrite) before calling SetLength
- If the stream is meant to be read-only, avoid resizing; copy data to a writable MemoryStream instead
- Catch NotSupportedException and fall back to a writable copy
Example fix
// before
readOnlyCfStream.SetLength(newSize);
// after
if (!readOnlyCfStream.CanWrite) {
var ms = new MemoryStream();
readOnlyCfStream.CopyTo(ms);
ms.SetLength(newSize);
} Defensive patterns
Strategy: validation
Validate before calling
if (!stream.CanWrite) throw new InvalidOperationException("Stream is read-only; SetLength is not allowed."); Type guard
static bool CanResize(Stream s) => s.CanWrite;
Try / catch
try { stream.SetLength(newLength); } catch (NotSupportedException) { /* copy to writable stream and retry */ } Prevention
- Check CanWrite before any mutating stream operation
- Open compound files with FileAccess.ReadWrite when resizing is needed
- Design read-only paths to avoid SetLength entirely
When it happens
Trigger: Calling SetLength on a CFStream obtained from a compound file opened read-only (e.g. FileInfo.OpenRead, FileAccess.Read, or a packaging stream opened with read access).
Common situations: Opening an .docx/.xlsx OLE compound file read-only and then trying to resize an embedded stream; code paths that assume the stream is writable because they were tested against FileStream in read-write mode.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- NotSupportedException
- SR.SetPositionNotSupported
- Stream does not support Write
- ArgumentOutOfRangeException(nameof(offset))
- can’t seek on baseStream
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6bfdfb79c53cbe38.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs:192
throw new System.ComponentModel.InvalidEnumArgumentException("origin", (int) origin, typeof(SeekOrigin));
}
_safeIStream.Seek( offset, translatedSeekOrigin, out seekPos );
return seekPos;
}
/// <summary>
/// See .NET Framework SDK under System.IO.Stream
/// </summary>
/// <param name="newLength">New length</param>
public override void SetLength( long newLength )
{
CheckDisposedStatus();
if (!CanWrite)
{
throw new NotSupportedException(SR.SetLengthNotSupported);
}
if( 0 > newLength )
{
throw new ArgumentOutOfRangeException(nameof(newLength),
SR.StreamLengthNegative);
}
_safeIStream.SetSize( newLength );
// updating the stream pointer if the stream has been truncated.
if (newLength < this.Position)
this.Position = newLength;
}
/// <summary>
/// See .NET Framework SDK under System.IO.Stream
/// </summary>View on GitHub (pinned to 81131a70a4)