FileStream Create

user609511 picture user609511 · Aug 21, 2012 · Viewed 47k times · Source

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?

Answer

CodeCaster picture CodeCaster · Aug 21, 2012

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

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

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

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