In the world of programming, file input/output (I/O) operations are fundamental. They allow your applications to interact with files on the disk, enabling you to read data from files and write data to them. This tutorial will guide you through the basics of file handling in C#, covering both reading from and writing to files.
C# provides several classes within the System.IO namespace to handle file I/O operations. The most commonly used classes for basic file operations are StreamReader, StreamWriter, File, and FileInfo.
To read text from a file, you can use StreamReader. Here's a simple example:
1using System;2using System.IO;34class Program5{6static void Main()7{8string filePath = "example.txt";910// Check if the file exists11if (File.Exists(filePath))12{13using (StreamReader reader = new StreamReader(filePath))14{15string content = reader.ReadToEnd();16Console.WriteLine(content);17}18}19else20{21Console.WriteLine("File does not exist.");22}23}24}
In this example, we first check if the file exists to avoid exceptions. If it does, we create a StreamReader object and read all the content of the file using ReadToEnd(). The using statement ensures that the StreamReader is properly disposed of after use.
To write text to a file, you can use StreamWriter. Here's how you can do it:
1using System;2using System.IO;34class Program5{6static void Main()7{8string filePath = "example.txt";9string contentToWrite = "Hello, world!";1011using (StreamWriter writer = new StreamWriter(filePath))12{13writer.WriteLine(contentToWrite);14}1516Console.WriteLine("Content written to file.");17}18}
In this example, we create a StreamWriter object and write a line of text to the file. The using statement ensures that the StreamWriter is properly disposed of after use.
If you want to append text to an existing file instead of overwriting it, you can open the file with the FileMode.Append mode:
1using System;2using System.IO;34class Program5{6static void Main()7{8string filePath = "example.txt";9string contentToAppend = "This is an appended line.";1011using (StreamWriter writer = new StreamWriter(filePath, true))12{13writer.WriteLine(contentToAppend);14}1516Console.WriteLine("Content appended to file.");17}18}
Here, the StreamWriter constructor's second parameter is set to true, which opens the file in append mode.
C# also provides static methods in the File class for reading and writing files. Here are some examples:
1using System;2using System.IO;34class Program5{6static void Main()7{8string filePath = "example.txt";910if (File.Exists(filePath))11{12string content = File.ReadAllText(filePath);13Console.WriteLine(content);14}15else16{17Console.WriteLine("File does not exist.");18}19}20}
1using System;2using System.IO;34class Program5{6static void Main()7{8string filePath = "example.txt";9string contentToWrite = "Hello, world!";1011File.WriteAllText(filePath, contentToWrite);1213Console.WriteLine("Content written to file.");14}15}
These methods are simpler and more concise for basic operations.
In the next section, we will explore streaming in C#, which is useful for handling large files or streams of data efficiently. Stay tuned!
Info
Always ensure that you handle exceptions properly when dealing with file I/O operations to avoid runtime errors.