17

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?

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
user609511
  • 4,091
  • 12
  • 54
  • 86

6 Answers6

14

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.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
10

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!

M. Jahedbozorgan
  • 6,914
  • 2
  • 46
  • 51
TrizZz
  • 1,200
  • 5
  • 15
  • 1
    I like this one slightly better, `File.Create` just wraps some defaults for the FileStream constructor, you maintain flexibility using `FileStream` should you need it later. Albeit at a slight readability penalty. – Grant H. Aug 21 '12 at 14:28
  • True, I prefer this way too, you obtain a better control with this way – TrizZz Aug 21 '12 at 14:33
  • @MAXE True! I was just writing this as an example, I corrected that! – TrizZz Aug 21 '12 at 14:35
  • @GrantH. watch out for YAGNI. Using your logic, one should always use the most extended constructor, for in case somewhere in the future maybe we want to change one of the parameters. If ever. If `File.Create` fits all requirements now, changing it to `new FileStream(path, other, options)` is really not that big an issue, while with `File.Create(path)` the intention immediately becomes clear. – CodeCaster Aug 21 '12 at 14:37
2

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

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

to manage default values of this method!

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

using (FileStream fs = new FileStream(strFilePath, FileMode.Create))
{
    // HERE WHAT YOU WANT TO DO!
}
MAXE
  • 4,978
  • 2
  • 45
  • 61
1

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.

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 (var fs = new FileStream(strFilePath, FileMode.Create))
{
    //do some stuff
}
PhonicUK
  • 13,486
  • 4
  • 43
  • 62
0

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.

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
0

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.

Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47