File Handling in C#: Reading and Writing Files


File handling is a common task in C# applications. In this guide, you'll learn how to read and write files, work with text and binary files, and handle common file-related operations.


Reading Text Files


Reading a text file is a straightforward process in C#. You can use the `System.IO` namespace to work with files.


Example of reading a text file:


using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "sample.txt";
try
{
// Read the entire content of the text file
string content = File.ReadAllText(filePath);
Console.WriteLine("File Content:\n" + content);
}
catch (IOException e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
}

Writing Text Files


You can also write to text files using the `File.WriteAllText` method.


Example of writing to a text file:


using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "output.txt";
try
{
string content = "This is some sample text to write to a file.";
File.WriteAllText(filePath, content);
Console.WriteLine("File written successfully.");
}
catch (IOException e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
}

Reading and Writing Binary Files


To work with binary files, you can use the `BinaryReader` and `BinaryWriter` classes from the `System.IO` namespace.


Example of reading and writing binary files:


using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "data.dat";
// Writing binary data to a file
using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
{
writer.Write(42);
writer.Write("Hello, Binary World!");
writer.Write(3.14159265359);
}
// Reading binary data from the file
using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
{
int integerData = reader.ReadInt32();
string textData = reader.ReadString();
double doubleData = reader.ReadDouble();
Console.WriteLine("Integer: " + integerData);
Console.WriteLine("Text: " + textData);
Console.WriteLine("Double: " + doubleData);
}
}
}

Common File Operations


C# provides various file-related operations, such as checking file existence, deleting files, copying files, and more. These operations are accessible via the `File` and `Directory` classes in the `System.IO` namespace.


Example of checking file existence and deleting a file:


using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "fileToDelete.txt";
// Check if a file exists
if (File.Exists(filePath))
{
Console.WriteLine("File exists. Deleting...");
File.Delete(filePath);
Console.WriteLine("File deleted.");
}
else
{
Console.WriteLine("File does not exist.");
}
}
}

Conclusion


File handling in C# is a crucial aspect of many applications. You've learned how to read and write text and binary files, as well as perform common file-related operations. Proper file handling can help you manage data effectively and interact with the file system in your applications.


Practice working with files in your C# programs to gain confidence in file handling operations. As you continue your programming journey, you'll explore more advanced file-related topics and libraries for handling different types of data and files.