FileStream 的FileShare一点小认识

C#读写文本文件一般都是用StreamWriter来实现(读书的时候就这样用。毕业后这几年基本也是这样干的),通常代码例如以下:

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

假设是web开发或则其它多线程的时候一般都是加锁(用lock),假设不同lock就会有error如:

这天我一同事推荐我说用FileStream能够不用lock,在多线程的情况下是不会有问题的,代码例如以下:

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

经过測试后发如今多线程下却实没有问题。于是回头查看了下面StreamWriter的定义。

复制代码
[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); }

复制代码

注意这里的CreateFile方法,里面用的是FileShare.Read,有关Read和 ReadWrite 的差别例如以下:     

Read 同意随后打开文件读取

假设未指定此标志。则文件关闭前,不论什么打开该文件以进行读取的请求(由此进程或还有一进程发出的请求)都将失败。但 是。即使指定了此标志。仍可能须要附加权限才可以訪问该文件。

 

ReadWrite 同意随后打开文件读取或写入。假设未指定此标志,则文件关闭前,不论什么打开该文件以进行读取或写入的请求(由此进程或还有一进程发出)都将失败。可是,即使指定了此标志,仍可能须要附加权限才可以訪问该文件。  

我想单独就FileShare属性大家都知道,但是在这里就StreamWriter和FileStream大家是否也知道这个问题了。有关StreamReader/StreamWriter与FileStream使用方法具体解释例如以下:

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

所以在涉及到文件操作的时候建议大家尽量用底层的FileStream操作

软件就是这样要不断学习、不断总结、不断前进。

原文地址:https://www.cnblogs.com/clnchanpin/p/6780347.html