Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are file operations in C#?
File operations in C# allow you to work with files and directories on the file system. These operations include creating, opening, reading, writing, appending, and deleting files. The System.IO namespace provides comprehensive classes and methods for file handling.
The FileStream class is the primary class for file operations in C#. It provides low-level access to files and derives from the abstract Stream class. This class enables reading from, writing to, and closing files efficiently.
Syntax
To create a FileStream object, use the following syntax −
FileStream fileStream = new FileStream(fileName, FileMode, FileAccess, FileShare);
FileMode Enumeration
The FileMode enumeration defines how the operating system should open a file −
Append − Opens an existing file and positions the cursor at the end, or creates the file if it doesn't exist.
Create − Creates a new file. If the file exists, it will be overwritten.
CreateNew − Creates a new file. If the file already exists, an exception is thrown.
Open − Opens an existing file. If the file doesn't exist, an exception is thrown.
OpenOrCreate − Opens a file if it exists; otherwise, creates a new file.
Truncate − Opens an existing file and truncates it to zero bytes.
FileAccess and FileShare Enumerations
The FileAccess enumeration specifies the type of access −
- Read − Read-only access
- Write − Write-only access
- ReadWrite − Both read and write access
The FileShare enumeration controls how the file can be accessed by other processes −
None − Denies sharing of the current file
Read − Allows subsequent opening of the file for reading
Write − Allows subsequent opening of the file for writing
ReadWrite − Allows subsequent opening of the file for reading and writing
Inheritable − Makes the file handle inheritable by child processes
Basic File Operations Example
Here's an example demonstrating how to create, write to, and read from a file using FileStream −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
FileStream fileStream = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
// Write bytes to file
for (int i = 1; i <= 20; i++) {
fileStream.WriteByte((byte)i);
}
// Reset position to beginning
fileStream.Position = 0;
// Read bytes from file
for (int i = 0; i <= 20; i++) {
Console.Write(fileStream.ReadByte() + " ");
}
fileStream.Close();
}
}
The output of the above code is −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1
Working with Text Files
For text file operations, C# provides StreamWriter and StreamReader classes that are more convenient than FileStream −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
// Writing to a text file
using (StreamWriter writer = new StreamWriter("sample.txt")) {
writer.WriteLine("Hello, World!");
writer.WriteLine("This is a sample text file.");
}
// Reading from a text file
using (StreamReader reader = new StreamReader("sample.txt")) {
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
}
}
The output of the above code is −
Hello, World! This is a sample text file.
File Operations Comparison
| Class | Purpose | Best For |
|---|---|---|
| FileStream | Low-level byte-based file operations | Binary files, precise control |
| StreamWriter | Writing text to files | Text files, easy writing |
| StreamReader | Reading text from files | Text files, line-by-line reading |
Conclusion
File operations in C# are handled through the System.IO namespace, with FileStream providing low-level access and StreamReader/StreamWriter offering convenient text file handling. Always remember to close file streams or use using statements to ensure proper resource cleanup.
