{"record":{"id":"d0fea3a63ff23df2","repo":"dotnet/wpf","slug":"sr-fileshareinvalid","errorCode":null,"errorMessage":"SR.FileShareInvalid","messagePattern":"SR\\.FileShareInvalid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":315,"sourceCode":"        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        }\n        else\n        {\n            throw new ArgumentException(\n                SR.FileShareInvalid);\n        }\n\n        if( 0 != (grfMode & SafeNativeCompoundFileConstants.STGM_CREATE))\n        {\n            // STGM_CREATE set, call StgCreateStorageEx.\n            returnValue = SafeNativeCompoundFileMethods.SafeStgCreateStorageEx(\n                path,\n                grfMode,\n                stgFormatDocFile,\n                0,\n                IntPtr.Zero,\n                IntPtr.Zero,\n                ref IID_IStorage,\n                out newRootStorage );\n        }\n        else\n        {","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L297-L333","documentation":"StorageRoot.Open maps FileShare values onto STGM share flags and throws ArgumentException when the share value is none of the supported ones (None, Read, Write, ReadWrite). Any other combination, e.g. Read | Write is fine but arbitrary combos like Delete or Read|Delete are not representable.","triggerScenarios":"Calling StorageRoot.Open / Package.Open with FileShare.Delete, FileShare.Read | FileShare.Delete, or other unsupported combinations.","commonSituations":"Passing through the share mode obtained from another FileStream; default parameters combined with Delete for temp-file semantics.","solutions":["Use one of FileShare.None, FileShare.Read, FileShare.Write, or FileShare.ReadWrite.","Do not pass FileShare.Delete to package opens; delete the container through Package/FileInfo instead.","Validate the share value before calling Package.Open."],"exampleFix":"// before\nPackage.Open(path, FileMode.Open, FileAccess.Read, FileShare.Delete);\n// after\nPackage.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);","handlingStrategy":"validation","validationCode":"FileShare[] supported = { FileShare.None, FileShare.Read, FileShare.Write, FileShare.ReadWrite };\nif (!supported.Contains(share))\n    share = FileShare.Read;","typeGuard":"bool IsSupportedFileShare(FileShare s) => s == FileShare.None || s == FileShare.Read || s == FileShare.Write || s == FileShare.ReadWrite;","tryCatchPattern":"try { pkg = Package.Open(path, mode, access, share); }\ncatch (ArgumentException) { pkg = Package.Open(path, mode, access, FileShare.Read); }","preventionTips":["Restrict share values to None/Read/Write/ReadWrite for packages.","Avoid FileShare.Delete with compound file opens."],"tags":["io","fileshare","argument"],"backgroundTag":"invalid-enum-value","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"}