"Go" C # How to share when reading a file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.