{"record":{"id":"679bf66631038077","repo":"dotnet/wpf","slug":"sr-documentstreamcannolongeropen","errorCode":null,"errorMessage":"SR.DocumentStreamCanNoLongerOpen","messagePattern":"SR\\.DocumentStreamCanNoLongerOpen","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/DocumentStream.cs","lineNumber":173,"sourceCode":"                    Trace.File,\n                    \"File copy failed -- reopening original file.\");\n                try\n                {\n                    Target = new FileStream(\n                        sourcePath,\n                        FileMode.Open,\n                        FileAccess.Read,\n                        FileShare.Read);\n                }\n                // If we fail to reopen the original file, rethrow an exception to\n                // indicate this specific error.\n                catch (Exception e)\n                {\n                    Trace.SafeWrite(\n                        Trace.File,\n                        \"Unable to reopen original file.\");\n\n                    throw new UnauthorizedAccessException(\n                        SR.DocumentStreamCanNoLongerOpen, e);\n                }\n            }\n            throw;\n        }\n\n        if (isFileSource)\n        {\n            Trace.SafeWrite(Trace.File, \"Performed a file copy from source.\");\n\n            // reacquire our stream\n            ReOpenWriteable();\n        }\n        else\n        {\n            // if the source wasn't a file, we want to copy the stream now\n            StreamHelper.CopyStream(this, target);\n            Trace.SafeWrite(Trace.File, \"Performed a stream copy from source.\");","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/DocumentStream.cs#L155-L191","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nstream.CopyTo(target);\n// after\ntry { stream.CopyTo(target); }\ncatch (UnauthorizedAccessException ex) { /* prompt to reopen the document */ }","handlingStrategy":"try-catch","validationCode":"var fi = new FileInfo(originalPath);\nbool readable = fi.Exists && CanOpenForRead(originalPath);","typeGuard":null,"tryCatchPattern":"try { stream.CopyTo(target); }\ncatch (UnauthorizedAccessException ex) { Log(ex); PromptReopenDocument(); }","preventionTips":["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."],"tags":["io","file-access","xps","unauthorized-access"],"backgroundTag":"file-read-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}