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?
Also, in some modes, a FileStream object creates directories when opening files. Missing directories are created when you instantiate a FileStream instance with the fileMode parameter of the FileStream() constructor set to FileMode. APPEND or FileMode. WRITE .
The Create() method of the File class is used to create files in C#. The File. Create() method takes a fully specified path as a parameter and creates a file at the specified location; if any such file already exists at the given location, it is overwritten.
The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With