How do i open a StreamReader
with FILE_SHARE_READ
, FILE_SHARE_WRITE
, FILE_SHARE_DELETE
?
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?
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:
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
?
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.