C# 文件流创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12056976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:14:27  来源:igfitidea点击:

FileStream Create

c#filestream

提问by user609511

Is this syntax

这是语法吗

 FileStream fs = new FileStream(strFilePath, FileMode.Create);

the same as this?

和这个一样吗?

FileStream fs = File.Create(strFilePath);

When yes, which one is better?

是的时候,哪个更好?

采纳答案by CodeCaster

It does matter, according to JustDecompile, because File.Createultimately calls:

根据 JustDecompile 的说法,这确实很重要,因为File.Create最终调用:

new FileStream(path, 
               FileMode.Create, 
               FileAccess.ReadWrite, 
               FileShare.None, 
               bufferSize, 
               options);

With a bufferSizeof 4096 (default) and FileOptions.None(also the same as with the FileStream constructor), but the FileShareflag is different: the FileStream constructor creates the Stream with FileShare.Read.

abufferSize为 4096(默认)和FileOptions.None(也与 FileStream 构造函数相同),但FileShare标志不同:FileStream 构造函数使用FileShare.Read.

So I say: go for readability and use File.Create(string)if you don't care about the other options.

所以我说:File.Create(string)如果你不关心其他选项,就去追求可读性和使用性。

回答by Renatas M.

First one creates or overwrites file with sharing Read access second with None. So it depends do you want to allow to give access while processing file or not.

第一个创建或覆盖文件,共享读取访问第二个与无。因此,这取决于您是否希望在处理文件时允许访问。

回答by TrizZz

In my opinion, I use this one:

在我看来,我使用这个:

using (FileStream fs = new FileStream(strFilePath, FileMode.Create))
{    
    fs.Write("anything");
    fs.Flush();
}

They basically doing the same thing, but this one create the file and opens it in create / write mode, and you can set your buffer size and all params.

他们基本上做同样的事情,但这个创建文件并以创建/写入模式打开它,您可以设置缓冲区大小和所有参数。

new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize, options);

With File.Create it wraps all those default buffer and params.. You will have a way better flexibility and management with my new FileStream(strFilePath, FileMode.Create); But at this point it's more a personnal choice, if you want more readability or management options!

使用 File.Create 它包装所有这些默认缓冲区和参数.. 使用我的新 FileStream(strFilePath, FileMode.Create); 您将获得更好的灵活性和管理方式。但在这一点上,如果您想要更多的可读性或管理选项,这更像是个人选择!

回答by PhonicUK

They do exactly the same thing. The only real difference is that the former would let you use a different FileMode at runtime if you wanted to (controlling it with a variable) and the latter will only ever be doing a Create operation.

他们做的事情完全一样。唯一真正的区别是前者可以让您在运行时使用不同的 FileMode(如果您愿意(用变量控制它)),而后者只会执行 Create 操作。

As a side note, convention is to handle things like a filestream in a using block to automatically dispose of them when they are out of scope.

作为旁注,约定是在 using 块中处理文件流之类的东西,以便在它们超出范围时自动处理它们。

using (var fs = new FileStream(strFilePath, FileMode.Create))
{
    //do some stuff
}

回答by ALI VOJDANIANARDAKANI

With the first one you have more options to do like : handle, file access, file mode, int buffer size,.... but with the second one you have less options to do.

对于第一个,您有更多选择,例如:句柄、文件访问、文件模式、int 缓冲区大小……但对于第二个,您可以做的选择更少。

回答by MAXE

The second one uses just a different FileMode for the stream: take a look to this article

第二个只对流使用不同的 FileMode:看看这篇文章

http://msdn.microsoft.com/en-us/library/47ek66wy.aspx

http://msdn.microsoft.com/en-us/library/47ek66wy.aspx

to manage default values of this method!

管理此方法的默认值!

But use a usingstatement, so any resource will be released in the correct way!

但是使用using声明,所以任何资源都会以正确的方式释放!

using (FileStream fs = new FileStream(strFilePath, FileMode.Create))
{
    // HERE WHAT YOU WANT TO DO!
}