Search code examples
c#filestream

FileStream Create


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?


Solution

  • 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.