的FileStream构造函数和默认缓冲区大小 [英] FileStream constructor and default buffer size

查看:150
本文介绍了的FileStream构造函数和默认缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在使用.NET 4的日志类写在C#我想补充,这将有选择地设置的 FileOptions.WriteThrough 标志构建的的FileStream 。由于这广泛应用于图书馆code我想改变尽可能少。

We have a logging class written in C# using .NET 4. I want to add a constructor argument which will optionally set the FileOptions.WriteThrough flag when constructing a FileStream. As this is widely used library code I want to change as little as possible.

现有的FileStream构造函数的调用:

_stream = new FileStream(_filePath, FileMode.Append, FileAccess.Write, FileShare.Read);

的问题:

要我们的构造我已经添加了一个名为writeDirectToDisk一个可选的布尔参数。我当时认为我可以做这样的事情:

To our constructor I have added an optional bool argument named writeDirectToDisk. I thought I'd then be able to do something like this:

var fileOptions = writeDirectToDisk ? FileOptions.WriteThrough : FileOptions.None;
_stream = new FileStream(_filePath, FileMode.Append, FileAccess.Write, FileShare.Read, fileOptions);

但没有,有没有这样的超载!除非我失去了一些东西,接受FileOptions参数供的FileStream的构造函数中唯一的重载还需要一个缓冲区大小参数!

but no, there's no such overload! Unless I'm missing something the only overloads available for FileStream's constructor which accept a FileOptions argument also require a buffer size argument!

我已经试过:

我已经尝试设置缓冲区大小为零希望将使用默认的缓冲区大小,但没有它抛出一个异常。

I've tried setting buffer size to zero hoping that would use the default buffer size but no it throws an exception.

我已经搜索并不能找到一些静态属性或常量,它指定默认的缓冲区大小是个什么框架。

I've searched for and can't find some static property or constant in the framework which specifies what the default buffer size is.

我的问题:

我不是在这个阶段特别困扰,有多少个字节的默认缓冲区大小为。我只是想知道我怎么能在FileOptions参数用尽可能少code影响,可能吗?

I'm not at this stage particularly bothered about how many bytes the default buffer size is. I just want to know how I can add the FileOptions argument to the constructor with as little code impact as possible?

我想知道如果有什么我已经错过了,我可以作为缓冲大小参数使用或某个常数或静止无功如果有过载我已经错过了,或事实上,如果有一些更聪明的方式来做到这一点。我也想知道,如果缓冲区大小是无关紧要的指定FileOptions.WriteThrough时,在这种情况下,我能做到这一点:

I'm wondering if there is some constant or static var which I've missed that I could use as the buffer size argument or if there's an overload I've missed or indeed if there's some smarter way to do this. I'm also wondering if the buffer size is irrelevant when FileOptions.WriteThrough is specified in which case I could do this:

            if (writeDirectToDisk)
            {
                _stream = new FileStream(_filePath, FileMode.Append, FileAccess.Write, FileShare.Read, 1, FileOptions.WriteThrough); // 1 is the smallest value allowed, it will actually be 8 bytes
            }
            else
            {
                _stream = new FileStream(_filePath, FileMode.Append, FileAccess.Write, FileShare.Read);
            }

但我宁愿不要,除非真的没有更好的方法。

but I'd rather not unless there's really no more elegant way.

推荐答案

您可以使用自己的工厂方法来构造的FileStream

You could use your own factory method to construct the FileStream.

除此之外,您还可以硬线使用反射(0×1000)发现了缓冲区的大小。

Other than that, you could hardwire the buffer size discovered using Reflector (0x1000).

这篇关于的FileStream构造函数和默认缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆