dotnet/wpf · error · ArgumentException
SR.FileShareInvalid
Error message
SR.FileShareInvalid
What it means
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.
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.
Example fix
// before Package.Open(path, FileMode.Open, FileAccess.Read, FileShare.Delete); // after Package.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
Defensive patterns
Strategy: validation
Validate before calling
FileShare[] supported = { FileShare.None, FileShare.Read, FileShare.Write, FileShare.ReadWrite };
if (!supported.Contains(share))
share = FileShare.Read; Type guard
bool IsSupportedFileShare(FileShare s) => s == FileShare.None || s == FileShare.Read || s == FileShare.Write || s == FileShare.ReadWrite;
Try / catch
try { pkg = Package.Open(path, mode, access, share); }
catch (ArgumentException) { pkg = Package.Open(path, mode, access, FileShare.Read); } Prevention
- Restrict share values to None/Read/Write/ReadWrite for packages.
- Avoid FileShare.Delete with compound file opens.
When it happens
Trigger: Calling StorageRoot.Open / Package.Open with FileShare.Delete, FileShare.Read | FileShare.Delete, or other unsupported combinations.
Common situations: Passing through the share mode obtained from another FileStream; default parameters combined with Delete for temp-file semantics.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- SR.FileShareUnsupported
- InvalidEnumArgumentException: invalid SeekOrigin value…
- SR.FileModeInvalid
- SR.FileModeUnsupported
- " }} " element found. Expected fixed page element ( }} ).
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d0fea3a63ff23df2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs:315
else if( share == FileShare.None ) // FileShare.None is zero, using "&" to check causes unreachable code error
{
grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE;
}
else if( share == FileShare.Read )
{
grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_WRITE;
}
else if( share == FileShare.Write )
{
grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_READ; // Note that this makes little sense when we don't support combination of flags
}
else if( share == FileShare.ReadWrite )
{
grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_NONE;
}
else
{
throw new ArgumentException(
SR.FileShareInvalid);
}
if( 0 != (grfMode & SafeNativeCompoundFileConstants.STGM_CREATE))
{
// STGM_CREATE set, call StgCreateStorageEx.
returnValue = SafeNativeCompoundFileMethods.SafeStgCreateStorageEx(
path,
grfMode,
stgFormatDocFile,
0,
IntPtr.Zero,
IntPtr.Zero,
ref IID_IStorage,
out newRootStorage );
}
else
{View on GitHub (pinned to 81131a70a4)