String sfilename = @ "C:\Exchange.dat";
System.IO.StreamReader file = new System.IO.StreamReader (sfilename);
String sTxt = file. ReadLine ();
File. Close ();
In this code, when C # reads a file, the default is read mode, which means that when it opens the file, the other application can only read the file and cannot modify it.
If you want another application to be able to modify the file when it is opened, you need to specify a mode of ReadWrite:
String sfilename = @ "C:\Exchange.dat";
FileStream fs = new FileStream (sFileName, FileMode.Open, FileAccess.Read, fileshare.readwrite);
System.IO.StreamReader file = new System.IO.StreamReader (FS);
String sTxt = file. ReadLine ();
File. Close ();
The sharing method here is FileShare, which gives other applications permission to manipulate the file. The values are none, Read, Write, ReadWrite, and so on.
None: decline to share the current file. Any request to open the file (a request made by this process or another process) will fail until the file is closed.
Read : allows file reads to be opened later. If this flag is not specified, any request to open the file for reading (a request made by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.
ReadWrite: allows subsequent open file reads or writes. If this flag is not specified, any request to open the file for reading or writing (emitted by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.
Write : allows subsequent open file writes. If this flag is not specified, any requests that open the file for writing (requests made by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file.
Feel like write,none of the effect, can not read and write. The file cannot be opened, what is the use of granting write permission! So when Fileshare.none, Fileshare.write, other applications do not have permission to open the file:
FileShare.Read, you can only open, but not modify:
"Go" C # How to share when reading a file