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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

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 …