{"record":{"id":"a8d4272ac4a4a22c","repo":"dotnet/wpf","slug":"sr-fileshareunsupported","errorCode":null,"errorMessage":"SR.FileShareUnsupported","messagePattern":"SR\\.FileShareUnsupported","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":294,"sourceCode":"            case FileMode.Truncate:\n                throw new ArgumentException(\n                    SR.FileModeUnsupported);\n            default:\n                throw new ArgumentException(\n                    SR.FileModeInvalid);\n        }\n\n        // Generate the access flags from the access parameter\n        SafeNativeCompoundFileMethods.UpdateModeFlagFromFileAccess( access, ref grfMode );\n\n        // Generate STGM from FileShare\n\n        // Note: the .NET SDK does not specify the proper behavior in reaction to\n        //  incompatible flags being sent in together.  Should ArgumentException be\n        //  thrown?  Or do some values \"trump\" others?\n        if( 0 != (share & FileShare.Inheritable) )\n        {\n            throw new ArgumentException(\n                SR.FileShareUnsupported);\n        }\n        else if( share == FileShare.None ) // FileShare.None is zero, using \"&\" to check causes unreachable code error\n        {\n            grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE;\n        }\n        else if( share == FileShare.Read )\n        {\n            grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_WRITE;\n        }\n        else if( share == FileShare.Write )\n        {\n            grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_READ; // Note that this makes little sense when we don't support combination of flags\n        }\n        else if( share == FileShare.ReadWrite )\n        {\n            grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_NONE;\n        }","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L276-L312","documentation":"StorageRoot.Open rejects FileShare.Inheritable because the underlying STGM share flags cannot express handle inheritance. Passing a share value containing the Inheritable bit throws ArgumentException.","triggerScenarios":"Calling StorageRoot.Open / Package.Open with share = FileShare.Inheritable or any combination that includes the Inheritable flag (e.g. FileShare.Read | FileShare.Inheritable).","commonSituations":"Copying share flags intended for FileStream onto package opens; code that passes the share mode of an inherited handle.","solutions":["Remove FileShare.Inheritable from the share value (mask it out: share & ~FileShare.Inheritable).","Use FileShare.Read, ReadWrite, or None as appropriate for the package.","Avoid forwarding raw share flags from unrelated file APIs to Package.Open."],"exampleFix":"// before\nPackage.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Inheritable);\n// after\nvar share = FileShare.Read | FileShare.Inheritable;\nPackage.Open(path, FileMode.Open, FileAccess.Read, share & ~FileShare.Inheritable);","handlingStrategy":"validation","validationCode":"if ((share & FileShare.Inheritable) != 0)\n    share &= ~FileShare.Inheritable;","typeGuard":"bool IsSupportedFileShare(FileShare s) => (s & FileShare.Inheritable) == 0;","tryCatchPattern":null,"preventionTips":["Strip FileShare.Inheritable before package opens.","Do not forward FileStream share flags blindly to Package.Open."],"tags":["io","fileshare","argument"],"backgroundTag":"unsupported-operation","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"}