FileShare: fileshare

Source: Internet
Author: User

FileShare: fileshare

C # Reading and Writing text files are generally implemented using StreamWriter (this is the case when reading a book, and this is basically the case after graduation). The Code is usually as follows:

Using (StreamWriter sw = new StreamWriter (logpath, true, Encoding. UTF8 ))
{
Sw. WriteLine (msg );
}

If it is web development or other multithreading, the lock is usually used (use lock). If it is different from the lock, there will be an error such:

On this day, a colleague recommended that I use FileStream instead of lock. The Code is as follows:

Using (FileStream fs = new FileStream (logpath, FileMode. Append, FileAccess. Write, FileShare. ReadWrite ))
{
Using (StreamWriter sw = new StreamWriter (fs ))
{
Sw. Write (msg );
}
}

After testing, I found that there is no problem with multithreading, so I checked the definition of StreamWriter below,

[SecurityCritical]internal StreamWriter(string path, bool append, Encoding encoding, int bufferSize, bool checkHost) : base(null){    if (path == null)    {        throw new ArgumentNullException("path");    }    if (encoding == null)    {        throw new ArgumentNullException("encoding");    }    if (path.Length == 0)    {        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));    }    if (bufferSize <= 0)    {        throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));    }    Stream streamArg = CreateFile(path, append, checkHost);    this.Init(streamArg, encoding, bufferSize, false);}[SecurityCritical]private static Stream CreateFile(string path, bool append, bool checkHost){    return new FileStream(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read, 0x1000, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost);}

Note that the CreateFile method here uses FileShare. Read. The differences between Read and ReadWrite are as follows:

Read allows subsequent open file reading. If this flag is not specified, before the file is closed, any requests that open the file for reading (requests sent by this process or another process) will fail. However, even if this flag is specified, you may still need additional permissions to access the file.

ReadWrite allows subsequent open files to be read or written. If this flag is not specified, any requests (sent by this process or another process) that open the file for reading or writing will fail before the file is closed. However, even if this flag is specified, you may still need to attach permissions to access the file.

I want to know all about the FileShare attribute, but do you know about StreamWriter and FileStream here. The usage of StreamReader, StreamWriter, and FileStream is described as follows:

Http://blog.csdn.net/sansan52048/article/details/9160995

Therefore, we recommend that you use the underlying FileStream whenever possible when file operations are involved. In this way, software must be continuously learned, summarized, and advanced.


Who will explain the meaning of this sentence?

FileStream fs = info. Open (FileMode. OenOrCreate,
FileAccess. ReadWrite, FileShare. ReadWrite );

Open the file and get the file stream.
FileMode. OpenOrCreate (open or create a file. If the specified file exists, open it; otherwise, create it)
FileAccess. ReadWrite (object access permission, which indicates readable and writable)
FileShare. ReadWrite (File Sharing permission, also readable and writable)

Filestream fs = new filestream ("atxt", filemodecreatenew, fileaccesswrite, filesharenone

Create a file stream object FS and read the.txt file under the program root directory. If the file does not exist, create a new file. The stream has the write access permission. If other processes or threads attempt to access the file during the access process, the request will be rejected.
We recommend that you go to msdn to search for information. Baidu knows that the efficiency of the. net function is still very low.
The following is the msdn address.
Msdn.microsoft.com/zh-cn/library/5h0z48dh.aspx

Related Article

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.