File handling in C#
File handling is an unmanaged resource in your application system. It is outside your application domain (unmanaged resource). It is not managed by CLR.
Data is stored in two ways, persistent and non-persistent manner.
When you open a file for reading or writing, it becomes stream.
Stream: Stream is a sequence of bytes traveling from a source to a destination over a communication path.
The two basic streams are input and output streams. Input stream is used to read and output stream is used to write.
The System.IO namespace includes various classes for file handling.
The parent class of file processing is stream. Stream is an abstract class, which is used as the parent of the classes that actually implement the necessary operations.
The primary support of a file as an object is provided by a .NET Framework class called File. This static class is equipped with various types of (static) methods to create, save, open, copy, move, delete, or check the existence of a file.
Diagram to represent file-handling class hierarchy
Note: FileIno, DirectoryInfo and DriveInfo classes have instance methods. File, Directory, Path classes have static methods.
The following table describes some commonly used classes in the System.IO namespace.
It is used to read from and write to any location within a file
It is used to read primitive data types from a binary stream
It is used to write primitive data types in binary format
It is used to read characters from a byte Stream
It is used to write characters to a stream.
It is used to read from a string buffer
It is used to write into a string buffer
It is used to perform operations on directories
It is used to perform operations on files
Reading and writing in the text file
The StreamWriter class in inherited from the abstract class TextWriter. The TextWriter class represents a writer, which can write a series of characters.
The following table describes some of the methods used by StreamWriter class.
Closes the current StreamWriter object and the underlying stream
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream
Writes to the stream
Writes data specified by the overloaded parameters, followed by end of line
Program to write user input to a file using StreamWriter Class
public void WriteData()
FileStream fs = new FileStream("c:\\test.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("Enter the text which you want to write to the file");
string str = Console.ReadLine();
static void Main(string args)
FileWrite wr = new FileWrite();
The StreamReader class is inherited from the abstract class TextReader. The TextReader class represents a reader, which can read series of characters.
The following table describes some methods of the StreamReader class.
Closes the object of StreamReader class and the underlying stream, and release any system resources associated with the reader
Returns the next available character but doesn't consume it
Reads the next character or the next set of characters from the stream
Reads a line of characters from the current stream and returns data as a string
Allows the read/write position to be moved to any position with the file
Program to read from a file using StreamReader Class
public void ReadData()
FileStream fs = new FileStream("c:\\test.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
Console.WriteLine("Program to show content of test file");
string str = sr.ReadLine();
while (str != null)
str = sr.ReadLine();
static void Main(string args)
FileRead wr = new FileRead();
I hope that this article would have helped you in understanding file handling.
Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.