Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added FileShare parameters to RootStorage.Open() to allow reading files used in other processes. #261

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions OpenMcdf.Tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}
}
9 changes: 5 additions & 4 deletions OpenMcdf/RootStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
Loading