Home > Net >  Use StreamReader constructor to open a TXT file, isn't open read-only?
Use StreamReader constructor to open a TXT file, isn't open read-only?

Time:09-22

Hi,

A simple but don't understand the problem, consult everybody,

C # write a console application, open a as follows. TXT file, read a line,

Using (StreamReader sr=new StreamReader (path))
{
The sr. ReadLine ();
}

Visual Studio single-step tracking, execute to the ReadLine this line, manually in the file manager to open the path corresponding TXT file, save changes, it can not save!


It.however, this is reader in c #, should be opened read-only, why other processes (such a file manager) used in the experiment, cannot modify saved?


If, in the c # code to achieve a pure "read only" to open a text file, is the new StreamReader (path) way not? So what to do?

Thank you, I wish the programmer the students a happy Spring Festival! ~ ~ ~

CodePudding user response:

Yes, the default FileShare set to FileShare. Read, can use the StreamReader (Stream) constructor
 
StreamReader sr=new StreamReader (new FileStream (path, FileMode. Open, FileAccess. Read, FileShare. ReadWrite))

CodePudding user response:

The
big watermelon a WuYiJin reference 1 floor? Response:
yes, set the default FileShare to FileShare. Read, can use the StreamReader (Stream) constructor
 
StreamReader sr=new StreamReader (new FileStream (path, FileMode. Open, FileAccess. Read, FileShare. ReadWrite))


You answer the questions never asked what

CodePudding user response:

You opened for direct reading though you can't write, but if you don't set share permissions to read and write, that others also can't write, so you need to open the file also set share permissions to read and write, other programs to write operations

CodePudding user response:

You have already read only, also let others how to write,,,,
Oww, know that you understand the deviation, not to say that you only read, this is a read-only for file, you only read no too big significance, not only their coding control is ok, isn't it

CodePudding user response:

refer to the second floor of orange peel ZZZ reply:
Quote: big watermelon a WuYiJin reference 1 floor? Response:

Yes, the default FileShare set to FileShare. Read, can use the StreamReader (Stream) constructor
 
StreamReader sr=new StreamReader (new FileStream (path, FileMode. Open, FileAccess. Read, FileShare. ReadWrite))


You answer this question never ask others what?
this can achieve the effect you want, don't understand without understanding is that some people are born for programmers,

CodePudding user response:

FileShare. ReadWrite refers to the current program can write, read
Multi-threaded read a TXT file, need to lock it, what's more, you this is across processes, file manager, and your program is two processes
Some of the documents, such as the MDB, because a DBMS in "maintenance", to support concurrency

CodePudding user response:

StreamReader sr=new StreamReader (new FileStream (path, FileMode. Open, FileAccess. Read, FileShare. ReadWrite))

This is sometimes also can't read, I met, I don't know why
  •  Tags:  
  • C#
  • Related