dotnet/wpf · error · NotSupportedException
SR.WriteNotSupported
Error message
SR.WriteNotSupported
What it means
VerifyStreamWriteArgs throws NotSupportedException when the stream does not support writing (CanWrite is false). WPF packaging streams (e.g. streams opened from read-only package parts) are often read-only, so any Write call fails before argument checks run.
Solutions
- Check stream.CanWrite before writing; if false, open the package/part in write mode (e.g. FileAccess.ReadWrite or FileMode.OpenOrCreate).
- Write to a temporary copy of the package, then replace the original if the source must stay read-only.
- Restructure code so writes go through Package/part APIs that open writable streams.
Example fix
// before using var part = package.GetPart(partUri); part.GetStream().Write(data, 0, data.Length); // read-only stream // after using var part = package.CreatePart(partUri, contentType, CompressionOption.Maximum); using var s = part.GetStream(FileMode.Create, FileAccess.Write); s.Write(data, 0, data.Length);
Defensive patterns
Strategy: validation
Validate before calling
if (!stream.CanWrite)
throw new InvalidOperationException("Stream is read-only; open the package part with FileAccess.Write."); Type guard
static bool IsWritable(Stream s) => s != null && s.CanWrite;
Try / catch
try { stream.Write(data, 0, data.Length); }
catch (NotSupportedException) { /* open part in write mode or copy package to temp and edit */ } Prevention
- Check CanWrite before any write loop on package streams.
- Open package parts with FileMode.Create/FileAccess.Write when editing content.
- Remember packages opened from read-only files yield read-only part streams.
When it happens
Trigger: Calling Write (or a wrapper using VerifyStreamWriteArgs) on a stream obtained from a package part opened in read mode, or a stream whose CanWrite returned false.
Common situations: Opening a ZipPackage part for reading but attempting to write updated content; writing to a package inside a file opened FileAccess.Read; trying to modify a package embedded in a resource.
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.ReadBufferTooSmall
- SR.ReadNotSupported
- SR.SetLengthNotSupported
- SR.SetPositionNotSupported
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0d51cd9608d630eb.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs:139
if (offset + count > buffer.Length)
{
throw new ArgumentException(SR.ReadBufferTooSmall, nameof(buffer));
}
}
}
/// <summary>
/// VerifyStreamWriteArgs
/// </summary>
/// <param name="s"></param>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <remarks>common argument verification for Stream.Write</remarks>
internal static void VerifyStreamWriteArgs(Stream s, byte[] buffer, int offset, int count)
{
if (!s.CanWrite)
throw new NotSupportedException(SR.WriteNotSupported);
ArgumentNullException.ThrowIfNull(buffer);
if (offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative);
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), SR.WriteCountNegative);
}
checked
{
if (offset + count > buffer.Length)
throw new ArgumentException(SR.WriteBufferTooSmall, nameof(buffer));
}View on GitHub (pinned to 81131a70a4)