Basics of FileStream in C# Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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); ParameterDescriptionFieldsName of the FileName 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"FileModeIt 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 bytesFileAccessIt 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 fileFileShareIt 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: Comment More infoAdvertise with us M ManasiKirloskar Follow Improve Article Tags : Programming Language C# CSharp-File-Handling Similar Reads Introduction of Object Oriented Programming As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functi 6 min read C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo 4 min read Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into 6 min read C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her 15+ min read C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean 5 min read C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search, 7 min read R Programming Language - Introduction R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti 4 min read C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application), 6 min read ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used 15+ min read Features of C Programming Language C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system.The main features of C language include low-level access to memory, a simple set of keywords, and a clean style 3 min read Like