{"record":{"id":"f693e57a9ded6ef9","repo":"dotnet/wpf","slug":"exception-of-type-unauthorizedaccessexception-was-thrown","errorCode":null,"errorMessage":"Exception of type 'UnauthorizedAccessException' was thrown.","messagePattern":"Exception of type 'UnauthorizedAccessException' was thrown\\.","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs","lineNumber":159,"sourceCode":"                                          (packageAccess== FileAccess.Read) ?  FileMode.Open: FileMode.OpenOrCreate,\n                                           packageAccess,\n                                          (packageAccess== FileAccess.Read) ?  FileShare.Read: FileShare.None\n                                          );\n                }\n                    \n                AddPackageToCache( _uri, package );               \n            }\n            else\n            {\n                //\n                // If either the previous opened package or\n                // this open request is not File Access Read\n                // throw UnauthorizedAccessException\n                //\n                if( packageAccess != FileAccess.Read ||\n                    package.FileOpenAccess != FileAccess.Read )\n                {\n                    throw new UnauthorizedAccessException();\n                }\n                AddPackageReference( _uri );\n            }\n            Initialize( package,\n                        compressionOption,\n                        streaming);\n        }\n\n        static XpsManager()\n        {\n            _globalLock = new Object();\n            _packageCache = new Dictionary<Uri,int>();\n        }\n        #endregion Constructors\n\n        #region Public properties\n\n        /// <summary>","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs#L141-L177","documentation":"The XpsManager constructor throws UnauthorizedAccessException when an existing package was opened with FileAccess.Read but the requested packageAccess is not Read (or the package's actual FileOpenAccess is not Read). Writing to a read-only opened package is not permitted, so access is denied.","triggerScenarios":"Constructing XpsManager/XpsDocument on an existing file/stream with FileAccess.Read while requesting PackageAccess.Write or ReadWrite; or the underlying package was opened read-only (file on disk is read-only) but write access was requested.","commonSituations":"Files pulled from read-only media or marked read-only; opening an XPS from a resource stream and then attempting to save; passing FileAccess.Read with a mismatched package open mode.","solutions":["Open the package/file with FileAccess.ReadWrite when you intend to modify the XPS document.","If read-only is intentional, pass FileAccess.Read (PackageAccess.Read) and never call write APIs.","Clear the OS read-only attribute on the file or copy it to a writable location first."],"exampleFix":"// before\nusing var doc = new XpsDocument(path, FileAccess.Read); // then writing\n// after\nusing var doc = new XpsDocument(path, FileAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"var fi = new FileInfo(path);\nif (fi.IsReadOnly) throw new UnauthorizedAccessException($\"{path} is read-only\");\n// open with FileAccess.ReadWrite when writing","typeGuard":null,"tryCatchPattern":"try { doc = new XpsDocument(stream, FileAccess.ReadWrite, compression); }\ncatch (UnauthorizedAccessException) { doc = new XpsDocument(stream, FileAccess.Read); /* read-only flow */ }","preventionTips":["Match the requested FileAccess to the intended usage before constructing XpsDocument.","Check file read-only attributes and stream capabilities up front."],"tags":["wpf","xps","file-access","permissions"],"backgroundTag":"permission-denied","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"}