C# File I/O4 min read

Files are used to store the data permanently in the storage device with a specific name and path. And when we open the file in order to read or write, it becomes a stream.

File Handling refers to the various operation that can be performed on a file such as reading from a file, writing to a file, appending file, etc. The two most common operations are reading from a file and writing to a file. And when we open a file to do so then the file becomes a stream.

The System.IO namespace class contains the input and output streams handling classes that are used for performing the various operation.

The members that are included in System.IO namespace classes are:

I/OClass Description
BinaryReaderReads primitive data from a binary stream.
BinaryWriterWrites primitive data in binary format.
BufferedStreamTemporary storage for a stream of bytes.
DirectoryHelps in manipulating a directory structure.
DirectoryInfoUsed for performing operations on directories.
DriveInfoProvides information for the drives.
FileHelps in manipulating files.
FileInfoUsed for performing operations on files.
FileStreamUsed to read from and write to any location in a file.
MemoryStreamUsed for random access to streamed data stored in memory.
PathPerforms operations on path information.
StreamReaderUsed for reading characters from a byte stream.
StreamWriterIs used for writing characters to a stream.
StringReaderIs used for reading from a string buffer.
StringWriterIs used for writing into a string buffer.

C# FileStream

As already discussed above, FileStream class provides a stream for the various file operations. Reading and writing data into a file becomes easy with FileStream.

However, a FileStream object is required in order to create a new file or open an existing one. To create a FileStream object we use a new keyword in the following way.

Let us see what each of the above Enumerator means:


This emulator defines the various ways through which we can open a file and its member are:

AppendIt creates a new file or opens a file(if exists any) and puts the cursor at the end of the file.
CreateIt is used to create a new file.
CreateNewThis member specifies the OS that it should create a new file.
Open It is used to open an existing file.
OpenOrCreateIt is used to open an existing file, if not then create a new one.
TruncateIt opens an existing file and truncates its size to zero bytes.


This enumerator has three members:

  • Read: Only to read.
  • ReadWrite: Both read and write
  • Write: Only write.


FileShare refers to sharing of files and its members are:

InheritableIt allows a filehandle to pass an inheritance to the child processes
NoneSharing of the current file is declined by this member.
ReadIt opens a file only for reading.
ReadWriteIt opens a file for both reading and writing
Write It opens a file only for writing.

Let us g through an example to see it in action.

Example: C# program for FileStream

The following program writes multiple bytes into the file

Output: Once the program is executed, a new file will be created if it doesn’t already exist. And inside that file, the following text will be present.



String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More