Essential C# File and Stream IO  Suggest an edit

Create File

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.


Table Of Contents
22 Enum
25 GUID
96 File and Stream IO
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close