Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileStream Create

Tags:

c#

filestream

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?

like image 279
user609511 Avatar asked Aug 21 '12 14:08

user609511


People also ask

Does FileStream create directory?

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 .

How do you create a file in C sharp?

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.

What is a FileStream in C#?

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.


1 Answers

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.

like image 91
CodeCaster Avatar answered Oct 07 '22 10:10

CodeCaster