diff --git a/OpenMcdf.Ole.Tests/OlePropertiesExtensionsTests.cs b/OpenMcdf.Ole.Tests/OlePropertiesExtensionsTests.cs index cb351e5..4f6c327 100644 --- a/OpenMcdf.Ole.Tests/OlePropertiesExtensionsTests.cs +++ b/OpenMcdf.Ole.Tests/OlePropertiesExtensionsTests.cs @@ -101,7 +101,7 @@ public void ModifyDocumentSummaryInformation() public void ReadSummaryInformationUtf8() { // Regression test for #33 - using var cf = RootStorage.Open("wstr_presets.doc", FileMode.Open); + using var cf = RootStorage.OpenRead("wstr_presets.doc"); using CfbStream stream = cf.OpenStream(PropertySetNames.SummaryInformation); OlePropertiesContainer co = new(stream); diff --git a/OpenMcdf.Tests/RootStorageTests.cs b/OpenMcdf.Tests/RootStorageTests.cs index 87dafc0..a118a20 100644 --- a/OpenMcdf.Tests/RootStorageTests.cs +++ b/OpenMcdf.Tests/RootStorageTests.cs @@ -3,6 +3,18 @@ [TestClass] public sealed class RootStorageTests { + [TestMethod] + [DoNotParallelize] // Test sharing + [DataRow("MultipleStorage.cfs")] + public void Open(string fileName) + { + using var rootStorage = RootStorage.OpenRead(fileName); + using var rootStorage2 = RootStorage.OpenRead(fileName); + + Assert.ThrowsException(() => RootStorage.Open(fileName, FileMode.Open)); + Assert.ThrowsException(() => RootStorage.OpenWrite(fileName)); + } + [TestMethod] [DataRow(Version.V3, 0)] [DataRow(Version.V3, 1)] diff --git a/OpenMcdf/RootStorage.cs b/OpenMcdf/RootStorage.cs index ab12773..8c7dd5d 100644 --- a/OpenMcdf/RootStorage.cs +++ b/OpenMcdf/RootStorage.cs @@ -70,6 +70,17 @@ public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags return Open(stream, flags); } + public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageModeFlags.None) + { + stream.ThrowIfNotSeekable(); + stream.Position = 0; + + IOContextFlags contextFlags = ToIOContextFlags(flags); + RootContextSite rootContextSite = new(); + _ = new RootContext(rootContextSite, stream, Version.Unknown, contextFlags); + return new RootStorage(rootContextSite, flags); + } + public static RootStorage OpenRead(string fileName, StorageModeFlags flags = StorageModeFlags.None) { ThrowIfLeaveOpen(flags); @@ -78,15 +89,12 @@ public static RootStorage OpenRead(string fileName, StorageModeFlags flags = Sto return Open(stream, flags); } - public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageModeFlags.None) + public static RootStorage OpenWrite(string fileName, StorageModeFlags flags = StorageModeFlags.None) { - stream.ThrowIfNotSeekable(); - stream.Position = 0; + ThrowIfLeaveOpen(flags); - IOContextFlags contextFlags = ToIOContextFlags(flags); - RootContextSite rootContextSite = new(); - _ = new RootContext(rootContextSite, stream, Version.Unknown, contextFlags); - return new RootStorage(rootContextSite, flags); + FileStream stream = File.OpenWrite(fileName); + return Open(stream, flags); } RootStorage(RootContextSite rootContextSite, StorageModeFlags storageModeFlags) diff --git a/StructuredStorageExplorer/MainForm.cs b/StructuredStorageExplorer/MainForm.cs index 9a89515..279d02b 100644 --- a/StructuredStorageExplorer/MainForm.cs +++ b/StructuredStorageExplorer/MainForm.cs @@ -142,7 +142,7 @@ private void LoadFile(string fileName) cf = null; // Load file - cf = RootStorage.Open(fileName, FileMode.Open); + cf = RootStorage.OpenWrite(fileName); fileNameLabel.Text = fileName; saveAsToolStripMenuItem.Enabled = true;