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:

FileMode:

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

ParametersDescription
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.

FileAccess:

This enumerator has three members:

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

FileShare:

FileShare refers to sharing of files and its members are:

ParametersDescription
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.

abcdefghijklmnopqrstuvwxyz


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More