dotnet/wpf · error · InvalidOperationException

SR.FileManagementStreamProxyIsReadOnly

Error message

SR.FileManagementStreamProxyIsReadOnly

What it means

StreamProxy's internal proxy property setter refuses to change the target stream when the proxy was created over a read-only target (_isTargetReadOnly). It throws InvalidOperationException(SR.FileManagementStreamProxyIsReadOnly).

Solutions

  1. Open the underlying file with FileAccess.ReadWrite so the proxy is not flagged read-only
  2. Ensure the file is not locked read-only by another process or by file attributes
  3. Avoid retargeting proxies over read-only streams; create a new writable proxy instead
  4. Catch InvalidOperationException and fall back to read-only document mode

Example fix

// before
var stream = File.OpenRead(path); // read-only target
var proxy = new StreamProxy(stream, isTargetReadOnly: true);
// after
var stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
var proxy = new StreamProxy(stream, isTargetReadOnly: false);
Defensive patterns

Strategy: validation

Validate before calling

if (proxy.IsTargetReadOnly) { /* do not retarget; open a writable copy */ }

Try / catch

try { proxy.Proxy = newStream; } catch (InvalidOperationException) { /* handle read-only proxy */ }

Prevention

When it happens

Trigger: Assigning the Proxy property (internally, during stream management) on a StreamProxy constructed over a read-only file or stream.

Common situations: Document opened read-only (file locked by another process or opened with FileAccess.Read) while the document manager attempts to swap or retarget the underlying stream.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/StreamProxy.cs:260

    #endregion Object Overrides

    #region Internal Properties
    //--------------------------------------------------------------------------
    // Internal Properties
    //--------------------------------------------------------------------------

    internal Stream Target
    {
        get { return _proxy; }

        set {
            if (!_isTargetReadOnly)
            {
                _proxy = value;
            }
            else
            {
                throw new InvalidOperationException(
                    SR.FileManagementStreamProxyIsReadOnly);
            }
        }
    }
        #endregion Internal Properties

        #region Private Fields
        //--------------------------------------------------------------------------
        // Private Fields
        //--------------------------------------------------------------------------

        private Stream _proxy;
        private bool _isTargetReadOnly;

    #endregion Private Fields
}
}

View on GitHub (pinned to 81131a70a4)