How to use C# FileStream class?

A stream for file operations such as read and write is provided by the FileStream class.

Create an object like this

FileStream fstream = new FileStream("d:\new.txt", FileMode.OpenOrCreate);

Above we have used FileMode.OpenOrCreate so that the file or opened or created if it does not already exist.

The following is n example showing how to use the FileStream class in C# −

using System;
using System.IO;

public class Demo {
   public static void Main(string[] args) {
      FileStream fstream = new FileStream("d:\new.txt", FileMode.OpenOrCreate);

      // write into the file
      fstream.WriteByte(90);

      // close the file
      fstream.Close();
   }
}
Updated on: 2020-06-20T15:27:34+05:30

394 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements