diff --git a/OpenMcdf.Tests/StorageTests.cs b/OpenMcdf.Tests/StorageTests.cs index e5e3d6d..52b3749 100644 --- a/OpenMcdf.Tests/StorageTests.cs +++ b/OpenMcdf.Tests/StorageTests.cs @@ -309,4 +309,19 @@ public void GetAndSetMetadata(Version version) Assert.AreEqual(1U, storage.StateBits); } } + + [TestMethod] + [DataRow("MultipleStorage.cfs")] + public void OpenReadOnlyWithSharing(string fileName) + { + using (var rootStorage = RootStorage.Open(fileName, FileMode.Open, FileAccess.ReadWrite, share: FileShare.ReadWrite)) + { + Assert.IsTrue(rootStorage.ContainsEntry("MyStorage")); + + using (var rootStorage2 = RootStorage.OpenRead(fileName, share: FileShare.ReadWrite)) + { + Assert.IsTrue(rootStorage2.ContainsEntry("MyStorage")); + } + } + } } diff --git a/OpenMcdf/RootStorage.cs b/OpenMcdf/RootStorage.cs index 498189b..ac5777b 100644 --- a/OpenMcdf/RootStorage.cs +++ b/OpenMcdf/RootStorage.cs @@ -92,7 +92,8 @@ public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags return Open(stream, flags); } - public static RootStorage Open(string fileName, FileMode mode, FileAccess access, StorageModeFlags flags = StorageModeFlags.None) + public static RootStorage Open(string fileName, FileMode mode, FileAccess access, FileShare share = FileShare.ReadWrite, + StorageModeFlags flags = StorageModeFlags.None) { if (fileName is null) throw new ArgumentNullException(nameof(fileName)); @@ -101,7 +102,7 @@ public static RootStorage Open(string fileName, FileMode mode, FileAccess access ThrowIfInvalid(access); ThrowIfLeaveOpen(flags); - FileStream stream = File.Open(fileName, mode, access); + FileStream stream = File.Open(fileName, mode, access, share); return Open(stream, flags); } @@ -119,14 +120,14 @@ public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageMo return new RootStorage(rootContextSite, flags); } - public static RootStorage OpenRead(string fileName, StorageModeFlags flags = StorageModeFlags.None) + public static RootStorage OpenRead(string fileName, FileShare share = FileShare.ReadWrite, StorageModeFlags flags = StorageModeFlags.None) { if (fileName is null) throw new ArgumentNullException(nameof(fileName)); ThrowIfLeaveOpen(flags); - FileStream stream = File.OpenRead(fileName); + FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read, share); return Open(stream, flags); }