dotnet/wpf · error · UnauthorizedAccessException
SR.DocumentStreamCanNoLongerOpen
Error message
SR.DocumentStreamCanNoLongerOpen
What it means
DocumentStream.Copy rethrows as UnauthorizedAccessException with SR.DocumentStreamCanNoLongerOpen when the original backing file cannot be reopened after a failure. The wrapper loses access to the underlying file (e.g. it was moved, deleted, or locked), so the stream can no longer serve reads and surfaces the original exception as inner.
Solutions
- Catch UnauthorizedAccessException around document open/copy operations and prompt the user to reopen the document from its original location.
- Ensure no external process (AV, sync tools) locks the XPS file while it is open; add exclusions for the app's temp directory.
- Retry the operation after the lock is released; keep the original file path available for reopening.
Example fix
// before
stream.CopyTo(target);
// after
try { stream.CopyTo(target); }
catch (UnauthorizedAccessException ex) { /* prompt to reopen the document */ } Defensive patterns
Strategy: try-catch
Validate before calling
var fi = new FileInfo(originalPath); bool readable = fi.Exists && CanOpenForRead(originalPath);
Try / catch
try { stream.CopyTo(target); }
catch (UnauthorizedAccessException ex) { Log(ex); PromptReopenDocument(); } Prevention
- Exclude the app's temp directory from antivirus/backup/sync interference.
- Do not move or delete the original document file while it is open.
- Persist the original path so the user can reopen after failure.
When it happens
Trigger: Using an XPS DocumentStream whose temporary/original file was deleted, moved, locked by another process, or whose permissions changed; the retry open in Copy fails and the exception is wrapped.
Common situations: Antivirus or backup tools temporarily locking the temp XPS file; users moving/deleting the document while the viewer has it open; temp-folder cleanup utilities removing files in use.
Understand the failure class
Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.
Related errors
- Exception of type 'UnauthorizedAccessException' was thrown.
- SR.ReachPackaging_CannotModifyReadOnlyContainer
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ContentType is not valid.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/679bf66631038077.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/DocumentStream.cs:173
Trace.File,
"File copy failed -- reopening original file.");
try
{
Target = new FileStream(
sourcePath,
FileMode.Open,
FileAccess.Read,
FileShare.Read);
}
// If we fail to reopen the original file, rethrow an exception to
// indicate this specific error.
catch (Exception e)
{
Trace.SafeWrite(
Trace.File,
"Unable to reopen original file.");
throw new UnauthorizedAccessException(
SR.DocumentStreamCanNoLongerOpen, e);
}
}
throw;
}
if (isFileSource)
{
Trace.SafeWrite(Trace.File, "Performed a file copy from source.");
// reacquire our stream
ReOpenWriteable();
}
else
{
// if the source wasn't a file, we want to copy the stream now
StreamHelper.CopyStream(this, target);
Trace.SafeWrite(Trace.File, "Performed a stream copy from source.");View on GitHub (pinned to 81131a70a4)