Home »
.Net
Introduction to file handling in C#
Learn: What is file handling in C#.Net? What are streams, type of streams, basic file operations and classes are used for file operations.
What is a file in C#?
File is a collection of data, which has unique name and stores data in specific path. When we open a file for reading or writing, it is known as stream.
What is Stream?
Stream is a sequence of bytes. There are two types of streams are used:
- Input stream
This stream is used to read data from file, which is known as read operation.
- Output stream
This stream is used to write data into file, which is known as write operation.
Namespace to use for file operations
In C#, System.IO namespace is used to perform file operations.
There are following file operations used in C#:
- Creation of file
- Deletion of file
- Reading data from file
- Writing data into file
- Appending data into file
- Opening a file
- Closing a file etc...
There are following classes are used file handling in C#:
- BinaryReader
- BinaryWriter
- BufferedStream
- Directory
- DirectoryInfo
- DriveInfo
- File
- FileInfo
- FileStream
- MemoryStream
- Path
- StreamReader
- StreamWriter
- StringReader
- StringWriter
There are following file modes used in C#:
- Open
- Create
- CreateNew
- OpenOrCreate
- Append
- Truncate