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
?
Read); //Create an object of StreamReader by passing FileStream object on which it needs to operates on StreamReader sr = new StreamReader(fs); //Use the ReadToEnd method to read all the content from file string fileContent = sr. ReadToEnd(); //Close the StreamReader object after operation sr. Close(); fs. Close();
C# StreamReader is used to read characters to a stream in a specified encoding. StreamReader. Read method reads the next character or next set of characters from the input stream. StreamReader is inherited from TextReader that provides methods to read a character, block, line, or all content.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With