{"record":{"id":"9b846d8c3e47bbb8","repo":"dotnet/wpf","slug":"stg-e-accessdenied","errorCode":"STG_E_ACCESSDENIED","errorMessage":"SR.CanNotCreateAccessDenied","messagePattern":"SR\\.CanNotCreateAccessDenied","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs","lineNumber":653,"sourceCode":"                            nativeCallErrorCode ));\n                }\n                */\n            // It doesn't already exist, please create.\n            StorageInfoCore newStorage = core.elementInfoCores[ name ] as StorageInfoCore;\n            Invariant.Assert( null != newStorage);\n    \n            int nativeCallErrorCode = core.safeIStorage.CreateStorage(\n                        name, \n                    (GetStat().grfMode & SafeNativeCompoundFileConstants.STGM_READWRITE_Bits)\n                        | SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE,\n                    0,\n                    0,\n                out newStorage.safeIStorage );\n            if( SafeNativeCompoundFileConstants.S_OK != nativeCallErrorCode )\n            {\n                if( nativeCallErrorCode == SafeNativeCompoundFileConstants.STG_E_ACCESSDENIED )\n                {\n                    throw new UnauthorizedAccessException(\n                            SR.CanNotCreateAccessDenied,\n                            new COMException( \n                            SR.Format(SR.NamedAPIFailure, \"IStorage.CreateStorage\"), \n                            nativeCallErrorCode ));\n                }\n                else\n                {\n                    throw new IOException(\n                        SR.UnableToCreateStorage,\n                        new COMException( \n                            SR.Format(SR.NamedAPIFailure, \"IStorage.CreateStorage\"), \n                            nativeCallErrorCode ));\n                }\n            }\n    \n            // Invalidate enumerators\n            InvalidateEnumerators();\n        }","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs#L635-L671","documentation":"Thrown by StorageInfo.CreateStorage when the underlying native IStorage.CreateStorage call returns STG_E_ACCESSDENIED. The library translates that HRESULT into UnauthorizedAccessException so callers can distinguish permission problems from other creation failures; the inner COMException preserves the HRESULT.","triggerScenarios":"Calling CreateStorage/CreateSubStorage on a compound file opened read-only, on a file locked by another process, or on a location without write permission.","commonSituations":"Opening an XPS/RM package from a read-only stream or network share, file opened while Windows Search/AV holds it, attempting to modify a package loaded from a Resource or readonly package stream.","solutions":["Open the compound file with FileAccess.ReadWrite and FileShare.Read (or none) instead of FileAccess.Read","Ensure the file and directory grant write permission to the current user and the file is not marked read-only","Close other handles locking the file (explorer preview, AV scanner, another app instance)","Copy the package to a writable location before modifying it"],"exampleFix":"// before\nvar root = StorageRoot.OpenOnFile(path, FileMode.Open, FileAccess.Read);\nroot.CreateSubStorage(\"newStorage\");\n// after\nvar root = StorageRoot.OpenOnFile(path, FileMode.Open, FileAccess.ReadWrite);\nroot.CreateSubStorage(\"newStorage\");","handlingStrategy":"try-catch","validationCode":"var fi = new FileInfo(path);\nif (!fi.Exists || fi.IsReadOnly || !CanWrite(path)) throw new UnauthorizedAccessException($\"Cannot write to {path}\");","typeGuard":null,"tryCatchPattern":"try { storage.CreateSubStorage(name); }\ncatch (UnauthorizedAccessException ex)\n{\n    var hresult = Marshal.GetHRForException(ex.InnerException as COMException);\n    // handle STG_E_ACCESSDENIED: reopen read-write or abort\n}","preventionTips":["Open compound files with FileAccess.ReadWrite when mutating","Handle STG_E_ACCESSDENIED explicitly to give users a clear message","Copy read-only packages to a temp writable path before editing"],"tags":["compound-file","access-denied","permissions","storage"],"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"}