C# File Operations, C# Input Output (C# I/O)
A file is a named collection of data stored on a storage device (like a hard disk, SSD, or USB drive). It allows data to persist even after a program stops running. Examples:
• document.txt → text data
• image.jpg → image data
• data.json → structured data
Inside a file, data is stored as:
• Text (human-readable) → .txt, .csv, .json
• Binary (machine-readable) → .exe, .jpg, .pdf
What is File I/O?
File I/O (Input/Output) means:
• Input → reading data from a file into a program
• Output → writing data from a program into a file
So File I/O is simply how a program communicates with files on disk.
Example:
• Read user data from users.txt
• Save results into output.txt
What are the file operations in C#?
In C#, file operations are actions you perform on files using the System.IO namespace.
Common file operations:
a) Create a file
File.Create("test.txt");
b) Write to a file
File.WriteAllText("test.txt", "Hello World");
c) Read from a file
string content = File.ReadAllText("test.txt");
Console.WriteLine(content);
d) Append to a file
File.AppendAllText("test.txt", "More text");
e) Delete a file
File.Delete("test.txt");
f) Work with streams (advanced)
using (StreamReader sr = new StreamReader("test.txt"))
{
string line = sr.ReadLine();
}
Required Namespaces to Enable c# File Operations
using System;
using System.IO;
How to Write File in C#?
C# Write File with TextWriter
TextWriter tw = new StreamWriter("date.txt");
tw.WriteLine(DateTime.Now);
tw.Close();
Writing to File with StreamWriter in C#
StreamWriter writer = new StreamWriter("c:\\test.txt");
writer.WriteLine("File created using StreamWriter class.");
writer.Close();
C# Flush String to File
File.WriteAllText("C:\\test.txt", "How C#");
Write String Array to File in C#
string[] howcsharp = new string[]
{
"c#",
"technology",
"comparison",
"algorithm",
"discussion",
"tutorial",
"faq"
};
File.WriteAllLines("file.txt", howcsharp);
C# File operations with FileStream
FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
for (int i = 1; i <= 20; i++)
{
F.WriteByte((byte)i);
}
F.Position = 0;
for (int i = 0; i <= 20; i++)
{
Console.Write(F.ReadByte() + " ");
}
F.Close();
Using BinaryWriter to write Binary File in C#
int[] integerList = new int[] { 1, 4, 6, 7, 11, 55, 777, 23, 266, 44, 82, 93 };
using (BinaryWriter b = new BinaryWriter(File.Open("file.bin",
FileMode.Create)))
{
foreach (int i in integerList)
{
b.Write(i);
}
}
How to Read From File in C#
C# Read From File with TextWriter
Textreader tr = new StreamReader("date.txt");
Console.WriteLine(tr.ReadLine());
tr.Close();
Reading From File with StreamReader in C#
using (StreamReader sr = File.OpenText("path"))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
Read Everything From File with ReadAllText Function in C#
string file = File.ReadAllText("C:\\file.txt");
Console.WriteLine(file);
And here is the code to return file content to a list in C#:
List<string> fileLines = File.ReadAllLines("file.txt").ToList();
Counting number of lines of a file in C#:
int lineCount = File.ReadAllLines("file.txt").Length;
Example LINQ Queries with File Operations:
bool exists = (from line in File.ReadAllLines("file.txt") where
line == "Some line match" select line).Count() > 0;
Reading File Content to A String Array in C#
string[] lines = File.ReadAllLines("file.txt");
foreach (string line in lines)
{
if (line.Length > 80)
{
Console.WriteLine(line);
}
}
Getting/Printing List of Directories in C#
string[] dirs = Directory.GetDirectories("c:\\folder\");
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
Getting/Printing List of Files in a Folder in C#
string[] files = Directory.GetFiles("c:\\folder\");
foreach (string file in files)
{
Console.WriteLine(file);
}
Binary File Operations, Using BinaryReader in C# to Read Binary Files
using (BinaryReader b = new BinaryReader(File.Open("file.bin",
FileMode.Open)))
{
int pos = 0;
int length = (int) b.BaseStream.Length;
while (pos < length)
{
int v = b.ReadInt32();
Console.WriteLine(v);
pos += sizeof(int);
}
}
File I/O in C# vs Other Languages
C#
• Uses System.IO library
• High-level helper methods (File.ReadAllText, File.WriteAllText)
• Also supports low-level streams (FileStream, StreamReader)
• Very structured and object-oriented
Python
with open("test.txt", "r") as f:
content = f.read()
• Very simple syntax
• Uses open() function
• File handling is very concise
Comparison: Python is easier for beginners, fewer lines of code
Java
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line = br.readLine();
• More verbose than C#
• Uses classes like FileReader, BufferedReader
• Requires explicit exception handling
Comparison: Similar to C#, but usually more boilerplate
C / C++
FILE *fp = fopen("test.txt", "r");
fgets(buffer, 100, fp);
fclose(fp);
• Very low-level file handling
• Manual memory and pointer management
• More error-prone but very fast
Comparison: C# is safer and higher-level than C/C++