Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a StreamReader in ShareDenyWrite mode?

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?

like image 413
Ian Boyd Avatar asked Sep 18 '12 13:09

Ian Boyd


People also ask

How do I use StreamReader?

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();

What is StreamReader function?

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.


1 Answers

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.

like image 141
Sean Carpenter Avatar answered Oct 03 '22 04:10

Sean Carpenter