Reading and Writing Files in C#
Introduction
File handling is a crucial part of many applications. In C#, the System.IO
namespace provides a wide range of classes for reading and writing files. This tutorial will cover the basic operations for handling files including reading from a file, writing to a file, and some advanced file handling techniques.
Reading Files
To read from a file in C#, you can use the StreamReader
class. This class provides methods to read strings, lines, and characters from a file. Below is an example of how to read the contents of a file:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
using (StreamReader sr = new StreamReader(path))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
In this example, we open the file example.txt
using a StreamReader
. We then read the file line by line until the end of the file is reached.
Writing Files
Writing to a file in C# can be done using the StreamWriter
class. This class provides methods to write strings, lines, and characters to a file. Below is an example of how to write to a file:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("Hello, World!");
sw.WriteLine("This is a sample text file.");
}
}
}
In this example, we open the file example.txt
for writing using a StreamWriter
. We then write two lines to the file. If the file already exists, it will be overwritten.
Appending to a File
To append data to an existing file, you can use the StreamWriter
class with the append parameter set to true
. Below is an example:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine("Appending a new line.");
}
}
}
In this example, we open the file example.txt
for appending. The new line will be added to the end of the file without overwriting the existing content.
Handling Exceptions
When working with file operations, it's important to handle exceptions to avoid crashes and ensure the program behaves correctly. The most common exceptions include FileNotFoundException
, IOException
, and UnauthorizedAccessException
. Below is an example of how to handle exceptions:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
try
{
using (StreamReader sr = new StreamReader(path))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (FileNotFoundException e)
{
Console.WriteLine("File not found: " + e.Message);
}
catch (IOException e)
{
Console.WriteLine("IO error: " + e.Message);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("Access denied: " + e.Message);
}
}
}
In this example, the file reading operation is wrapped in a try-catch block to handle potential exceptions that may occur during the file operation.
Conclusion
In this tutorial, we covered the basics of reading and writing files in C#. We explored how to use StreamReader
and StreamWriter
classes, how to append data to a file, and how to handle exceptions during file operations. With these tools, you can effectively manage file I/O in your C# applications.