【问题标题】:Buffer size for File.OpenReadFile.OpenRead 的缓冲区大小
【发布时间】:2013-09-06 12:02:55
【问题描述】:

如果您使用FileStream 构造函数之一,您可以指定缓冲区大小(以字节为单位),但如果您使用File.OpenRead,则不能。第二种情况下使用的缓冲区大小的默认值是多少?

【问题讨论】:

    标签: c# .net io


    【解决方案1】:

    用Telerik JustDecompile看代码,是4096 B:

    public static FileStream OpenRead(string path)
    {
        return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
    }
    
    public FileStream(string path, FileMode mode, FileAccess access, FileShare share) : this(path, mode, access, share, 4096, FileOptions.None, Path.GetFileName(path), false)
    {
    }
    

    【讨论】:

    • +1 是一个很好的运动员朋友。也是获得答案的好方法。 :D
    【解决方案2】:

    它是4096,你可以从这个构造函数中看到:

    [SecuritySafeCritical]
    public FileStream(string path, FileMode mode, FileAccess access, FileShare share)
        : this(path, mode, access, share, 4096,
               FileOptions.None, Path.GetFileName(path), false)
    {
    }
    

    这就是OpenRead调用的构造函数:

    public static FileStream OpenRead(string path)
    {
        return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
    }
    

    【讨论】:

    • 仅供参考:请注意,4K 是用于最大 16TB 大小的 NTFS 磁盘的默认集群大小,这可能是 MS 选择它作为 FileStream() 的默认大小的原因。 support.microsoft.com/kb/140365
    • @MatthewWatson,是的,我同意你的看法。
    • 感谢您的回复。但是@w128 比你早回答。希望我能接受这两个答案
    • 其实,我认为应该指出,他似乎早点回答了。
    • @w128,我发现你的答案有 2 分钟的差异,对你有利
    猜你喜欢
    • 2020-09-05
    • 2014-06-10
    • 2015-06-05
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    相关资源
    最近更新 更多