In .NET Framework, the System.IO namespace contains classes and functions that are used for file-related functionality, such as creating, reading, writing, updating, or closing a file. In this article, we will look at the FileStream class and its importance in WinForms applications.

Using FileStream Class in C# for file operations

It is a recommended programming practice to use FileStream with the using statement, which ensures that the Dispose method is executed even when there is an exception being thrown by the code. The Dispose method takes care of managed and unmanaged resources. If you don’t dispose-off the stream, the code has to rely on the garbage collector to free the FileStream instance and close the file.

Let’s take a look at few examples to open a file using FileStream.

To use FileStream, first, you have to create an instance of the class as follows.

FileStream file = new FileStream ("MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.Read);

The FileMode enumerator explains the various modes for the file while .NET tries to open it.
For example,

  • Append will open an existing file and take the cursor to the end-of-the-file, or it will create a new file if the file doesn’t exist already.
  • Open will open an existing file.
  • Truncate will open an existing file while truncating everything written in the file.

Open existing file read and write operations

        byte[] data = new UTF8Encoding(true).GetBytes("");
        using ( FileStream fileStream   = new FileStream(@"MyFile.txt", FileMode.Open))
            fileStream.Write(data, 0, data.Length);

Open existing file for read-only

        using (var fileStream = new FileStream("MyFile.txt", FileMode.Open, FileAccess.Read))
            byte[] b = new byte[1024];
            UTF8Encoding data = new UTF8Encoding(true);
            while (fileStream.Read(b, 0, b.Length) > 0)

Open file for writing

Opening a file with write mode will create a new file if the file you are trying to access doesn’t exist.

        using (var fileStream = new FileStream("MyFile.txt", FileMode.Open, FileAccess.Write))
            // write to file

Open file for writing with seek to end

If you are using FileMode.Append, this will create a new file if that doesn’t exist. The FileMode.Append is used to start writing data from the end of the file content if exist.

        using (var fileStream = new FileStream("MyFile.txt", FileMode.Append))
            // append to file

Open a file after overwriting an existing one

The code below will overwrite a file with a new one if it already exists.

        using (var fileStream = new FileStream("MyFile.txt", FileMode.Create))
            // write to just created file

Open a file after creating it

If you want a new file to be created instead of opening one, you can use the CreateNew file mode. It will throw an exception if there is already a file with the same name.

        using (var fileStream = new FileStream("MyFile.txt", FileMode.CreateNew))
            // Do stuff with the file just created

Related Articles

Last modified: September 17, 2019


Write a Reply or Comment

Your email address will not be published.