问题
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
?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/12478512/how-to-open-a-streamreader-in-sharedenywrite-mode