How to open a StreamReader in ShareDenyWrite mode?
Asked Answered
M

2

6

How do i open a StreamReader with FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_DELETE?


Same question, slightly expanded

How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can read the file?

How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can modify the file while i'm reading it?

How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can delete the file while i'm reading it?


Same question, slightly more expanded

In the .NET Framework class library there is a class called StreamReader. It is the only class designed to read "text", which is why it descends from the abstract base TextReader class. The TextReader/StreamReader allows you to specify the encoding used by the file you are trying to open, and can decode the file for you, returning Strings of text.

Once i've opened a file with the StreamReader:

var sr = new StreamReader(path);

The file is locked, with other processes unable to modify or delete the file. What i need is the equivalent of a FileStream class's FileShare enumeration:

  • None: Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.
  • Read": Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
  • Write: Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
  • ReadWrite:Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
  • Delete: Allows subsequent deleting of a file.

Except that, for obvious reasons, i cannot use a FileStream - have to use a StreamReader.

How can i open a StreamReader with FileShare.ReadWrite | FileShare.Delete?

Medlin answered 18/9, 2012 at 13:57 Comment(0)
A
11

StreamReader has a constructor that can take a stream. So instead of using the constructor that takes a string path, first create a FileStream with the options that you want, then pass that FileStream to the StreamReader constructor.

Angus answered 18/9, 2012 at 14:1 Comment(0)
L
7

How can i open a StreamReader with FileShare.ReadWrite | FileShare.Delete ?

When you have solved the problem for a Stream, the Reader is easy:

var fs = new FileStream(fileName, FileMode.Open, FileShare.ReadWrite|FileShare.Delete);
var sr = new StreamReader(fs);

And of course that should be wrapped in a using() { } block.

Lucre answered 18/9, 2012 at 14:1 Comment(4)
Sorry Henk, but Sean needs the rep more than you :)Medlin
At the C# 4.5 I see the usage as var fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete); namely they added another parameter between FileMode.Open and FileShare.ReadWrite, which is FileAccess..Godiva
@Godiva - I don't think that is new, it has multiple overloads.Lucre
I mean I could not see a constructor with only FileShare parameter, they all want FileAccess parameter if FileShare is wanted to be used. msdn.microsoft.com/en-us/library/…Godiva

© 2022 - 2024 — McMap. All rights reserved.