Create File

suggest change

File static class

By using Create method of the File static class we can create files. Method creates the file at the given path, at the same time it opens the file and gives us the FileStream of the file. Make sure you close the file after you are done with it.

ex1:

var fileStream1 = File.Create("samplePath");
/// you can write to the fileStream1
fileStream1.Close();

ex2:

using(var fileStream1 = File.Create("samplePath"))
{
    /// you can write to the fileStream1
}

ex3:

File.Create("samplePath").Close();

FileStream class

There are many overloads of this classes constructor which is actually well documented here. Below example is for the one that covers most used functionalities of this class.

var fileStream2 = new FileStream("samplePath", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

You can check the enums for FileMode, FileAccess, and FileShare from those links. What they basically means are as follows:

FileMode: Answers “Should file be created? opened? create if not exist then open?” kinda questions.

FileAccess: Answers “Should I be able to read the file, write to the file or both?” kinda questions.

FileShare: Answers “Should other users be able to read, write etc. to the file while I am using it simultaneously?” kinda questions.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


File and Stream IO:
* Create File

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
95 File and Stream IO
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script