Basics of FileStream in C#
Last Updated :
28 Apr, 2025
The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare. The Syntax to declare a FileStream object is given as
FileStream fileObj = new FileStream(file Name/Path, FileMode.field, FileAccess.field, FileShare.field);
Parameter | Description | Fields |
---|
Name of the File | Name of the file you want to work with along with its extension or the complete path of the file. | Eg: FileName.txt, @"C:\Users\Username\Documents\FileName.txt" |
FileMode | It specifies which mode the file has to be opened in. | - Open - To open an existing file - Create - To create a new file if the same file name already exists it will be overwritten - OpenOrCreate - To open a file if it exists else create new if it doesn't - Create - To specifically create a new file - Append - To open an existing file and append more information at the end of the file. If the file doesn't exist a new file will be created - Truncate - To open a existing file and truncate its size to Zero bytes |
FileAccess | It specifies the access to the file. | - Read - To read data from a file - Write - To write data to a file - ReadWrite - To read and write data to a file |
FileShare | It specifies the access given to other FileStream objects to this particular file | - None - To decline the sharing of the file. Any access request will fail until the file is closed. - Read - To allow subsequent reading of the file. - Write - To allow subsequent writing to the file. - ReadWrite - To allow subsequent reading and writing of the file. - Delete - To allow subsequent deleting of the file. - Inheritable - To allow the file handle inheritable by child processes. |
Example: In the code given below we write and read some text to a text file. To write the text first create an object of the FileStream class in Create mode and Write access. Store the text you want to write in a variable of type var, it is a keyword used to declare implicit types. Next, create a byte array and encode the text into UTF8 which is an encoding standard capable of encoding all 1, 112, 064 valid character code points in Unicode. Then using the Write() method write to the text file. The Write() method's parameters are the byte array to write from, the offset of the text file, and the length of the text. Lastly, close the FileStream object using Close(). To read the text file we create a FileStream object in Open mode and Read access. Declare a byte array to read from the text file and an integer to keep the count of the bytes. Using the Read() method read from the text file. The Read() method's parameters are the byte array, offset of the text file from where to begin reading, and the length of the text that has to be read. Lastly using GetString() write the read text from the byte array to the console.
csharp
// C# program to write and read from
// a text file using FileStream class
using System;
using System.IO;
using System.Text;
namespace FileStreamWriteRead {
class GFG {
static void Main(string[] args)
{
// Create a FileStream Object
// to write to a text file
// The parameters are complete
// path of the text file in the
// system, in Create mode, the
// access to this process is
// Write and for other
// processes is None
FileStream fWrite = new FileStream(@"M:\Documents\Textfile.txt",
FileMode.Create, FileAccess.Write, FileShare.None);
// Store the text in the variable text
var text = "This is some text written to the textfile "+
"named Textfile using FileStream class.";
// Store the text in a byte array with
// UTF8 encoding (8-bit Unicode
// Transformation Format)
byte[] writeArr = Encoding.UTF8.GetBytes(text);
// Using the Write method write
// the encoded byte array to
// the textfile
fWrite.Write(writeArr, 0, text.Length);
// Close the FileStream object
fWrite.Close();
// Create a FileStream Object
// to read from a text file
// The parameters are complete
// path of the text file in
// the system, in Open mode,
// the access to this process is
// Read and for other processes
// is Read as well
FileStream fRead = new FileStream(@"M:\Documents\Textfile.txt",
FileMode.Open, FileAccess.Read, FileShare.Read);
// Create a byte array
// to read from the
// text file
byte[] readArr = new byte[text.Length];
int count;
// Using the Read method
// read until end of file
while ((count = fRead.Read(readArr, 0, readArr.Length)) > 0) {
Console.WriteLine(Encoding.UTF8.GetString(readArr, 0, count));
}
// Close the FileStream Object
fRead.Close();
Console.ReadKey();
}
}
}
Output: 
Similar Reads
Basics of File Handling in C# Generally, the file is used to store the data. The term File Handling refers to the various operations like creating the file, reading from the file, writing to the file, appending the file, etc. There are two basic operation which is mostly used in file handling is reading and writing of the file.
3 min read
Basics Operations of File and Directory in C# In this article, we are going to cover how to create, delete and rename directory and also how to delete and rename the file.  Creating a Directory We can create Directory using CreateDirectory() method present in the Directory class. csharp // C# program to create a directory using System; using
4 min read
Implementing Binary Reader Using C# BinaryReader is a class that is present under the System.IO namespace. This is used for handling the binary values from a particular encoding stream. Syntax: This will create the BinaryReader object for a particular input stream by using UTF-8 encoding. BinaryReader br = new BinaryReader(Stream) Thi
2 min read
C# - FileInfo Class Methods In this article, we will explain the FileInfo class, its methods, properties, etc. The System.IO namespace is one of the most important namespaces we used while we are working with files in C#. FileInfo Class:It does not have static methods and it can only be used on instantiated objects. The class
3 min read
C# Program to Search Directories and List Files Given files and directories, now our task is to search these files and directories using C#. So to do this task we use the following methods: 1. SearchOption: This method is used to specify whether to search the current directory or the current directory with all subdirectories. Syntax: public enum
2 min read
File.GetCreationTime() Method in C# with Examples File.GetCreationTime(String) is an inbuilt File class method which is used to return the creation date and time of the specified file or directory. Syntax: public static DateTime GetCreationTime (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the
2 min read