Create File

suggest change

File static class

File.Create creates a file at the given path, opens it, and returns its FileStream. Close the stream when 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

This class has many constructor overloads, documented here. The example below is a commonly used one.

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

The FileMode, FileAccess, and FileShare enums mean:

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:



Table Of Contents