The System.IO namespace is a crucial part of the .NET framework, providing a standardized way to interact with the operating system's file system. It offers a consistent set of classes and methods for common tasks like file operations, directory management, and more.
Understanding these classes is fundamental to building robust and reliable applications.
Key classes include:
- `File`: Represents a file object.
- `Directory`: Represents a directory object.
- `CreateFile`: Creates a new file.
- `ReadFile`: Reads data from a file.
- `WriteFile`: Writes data to a file.
- `DeleteFile`: Deletes a file.
Here's a simple example of reading a file:
using System;
public class Example {
public static void Main(string[] args) {
string filePath = "C:\\temp\\example.txt";
try {
string content = File.ReadAllText(filePath);
Console.WriteLine(content);
} catch (FileNotFoundException e) {
Console.WriteLine($"File not found: {filePath}");
}
}