How to open StreamReader in ShareDenyWrite mode?

How can I open StreamReader with FILE_SHARE_READ , FILE_SHARE_WRITE , FILE_SHARE_DELETE ?


Same question slightly expanded

How to open StreamReader so that I can read the encoded text file with sharing options so that another process can read the file?

How can I open StreamReader so that I can read the encoded text file with sharing options so that another process can change the file while I read it?

How to open StreamReader so that I can read the encoded text file with sharing options so that another process can delete the file while I read it?


Same question a little more advanced

The .NET Framework class library has a class called StreamReader . This is the only class designed to read "text" , so it comes off the abstract base class TextReader . TextReader/StreamReader allows TextReader/StreamReader to specify the encoding used by the file you are trying to open, and can decode the file for you by returning Strings text.

As soon as I opened the file with StreamReader :

 var sr = new StreamReader(path); 

The file is locked, and other processes cannot modify or delete the file. What I need is equivalent to the FileStream class FileShare enumeration:

  • No : cancels sharing of the current file. Any request to open a file (by this process or another process) will fail with an error until the file is closed.
  • Read ": allows the 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 with the error until the file but even if this flag is specified, to access the file additional permissions may be required.
  • Write Allows you to continue opening the file for writing. If this flag is not specified, any request to open a file for writing (by this process or another process) will fail with an error until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.
  • ReadWrite . Allows you to continue opening the file for reading or writing. If this flag is not specified, any request to open a file for reading or writing (by this process or another process) will fail with an error until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.
  • Delete Allows you to subsequently delete the file.

Also, for obvious reasons, I cannot use FileStream - use StreamReader .

How can I open StreamReader using FileShare.ReadWrite | FileShare.Delete FileShare.ReadWrite | FileShare.Delete ?

+4
source share
2 answers

StreamReader has a constructor that can receive a stream. Therefore, instead of using a constructor that takes a string path, first create a FileStream with the necessary parameters, then pass this FileStream to the StreamReader constructor.

+9
source

How can I open StreamReader using FileShare.ReadWrite | FileShare.Delete?

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

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

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

+3
source

Source: https://habr.com/ru/post/1434852/


All Articles