dotnet/wpf · error · InvalidOperationException
Cannot remove signature from read-only file.
Error message
Cannot remove signature from read-only file.
What it means
PackageDigitalSignatureManager.RemoveSignature(Uri) refuses to modify a package opened in read-only mode. The manager exposes ReadOnly and throws InvalidOperationException before touching the signatures, since a read-only Package cannot have parts or relationships deleted. Caller should call Package.Flush() afterward to persist successful removals.
Solutions
- Reopen the package with FileAccess.ReadWrite (e.g. Package.Open(path, FileMode.Open, FileAccess.ReadWrite)) before calling RemoveSignature.
- Check PackageDigitalSignatureManager.ReadOnly before calling and surface a clear message or copy the package to a writable location first.
- If the source file itself is locked or on read-only media, copy it locally, clear the read-only attribute, and open read-write.
Example fix
// before using Package pkg = Package.Open(path, FileMode.Open, FileAccess.Read); var mgr = new PackageDigitalSignatureManager(pkg); mgr.RemoveSignature(sigUri); // InvalidOperationException // after using Package pkg = Package.Open(path, FileMode.Open, FileAccess.ReadWrite); var mgr = new PackageDigitalSignatureManager(pkg); mgr.RemoveSignature(sigUri); pkg.Flush();
Defensive patterns
Strategy: validation
Validate before calling
if (mgr.ReadOnly) throw new InvalidOperationException("Package is read-only; reopen with FileAccess.ReadWrite before removing signatures.");
mgr.RemoveSignature(signatureUri); Type guard
bool CanModify(PackageDigitalSignatureManager mgr) => !mgr.ReadOnly;
Try / catch
try { mgr.RemoveSignature(uri); pkg.Flush(); }
catch (InvalidOperationException ex) { /* read-only package: reopen writable or report */ } Prevention
- Always open packages with FileAccess.ReadWrite when the workflow mutates signatures.
- Check ReadOnly before any Remove*/Sign call.
- Remember to call Package.Flush() after mutation or changes stay in memory.
When it happens
Trigger: Calling RemoveSignature(uri) on a manager whose underlying Package was opened with FileAccess.Read or FileMode.Open + FileAccess.Read, leaving ReadOnly == true.
Common situations: Opening a .oxps/.docx-like OPC package from a read-only share, from a stream the caller only has read access to, or with Package.Open(path, FileMode.Open, FileAccess.Read) and then trying to edit signatures.
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
- ' ' ID is not a valid XSD ID.
- Cannot sign read-only file.
- Image_OriginalStreamReadOnly
- NotSupportedException
- Object identifiers must be unique within the same signature.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/8f8856a718b47623.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/PackageDigitalSignatureManager.cs:687
if (exitOnFailure)
break;
}
}
}
return result;
}
/// <summary>
/// Remove a signature
/// </summary>
/// <param name="signatureUri">signature to remove</param>
/// <remarks>Caller should call Package.Flush() in order to persist changes.</remarks>
public void RemoveSignature(Uri signatureUri)
{
if (ReadOnly)
throw new InvalidOperationException(SR.CannotRemoveSignatureFromReadOnlyFile);
ArgumentNullException.ThrowIfNull(signatureUri);
// empty?
if (!IsSigned) // calls EnsureSignatures for us
return;
// find the signature
int index = GetSignatureIndex(signatureUri);
if (index < 0)
return;
try
{
Debug.Assert(index < _signatures.Count);
//After this signature is removed the total number of signatures remaining will
//be _signatures.Count - 1. If this count is zero, then additional clean up needsView on GitHub (pinned to 81131a70a4)