Module 5 - Chapter 1: File Handling in C#

File handling is an essential aspect of many C# applications, enabling you to create, read, update, and delete files on the filesystem. C# provides a comprehensive set of classes in the System.IO namespace for working with files and directories, making file operations straightforward and efficient. This chapter covers the basics of file handling in C#, including how to work with files and directories, read and write files, and handle file I/O operations securely and efficiently.

Mar 29, 2024

File Handling in C#

File handling is an essential aspect of many C# applications, enabling you to create, read, update, and delete files on the filesystem. C# provides a comprehensive set of classes in the System.IO namespace for working with files and directories, making file operations straightforward and efficient. This chapter covers the basics of file handling in C#, including how to work with files and directories, read and write files, and handle file I/O operations securely and efficiently.

Working with the System.IO Namespace

The System.IO namespace contains classes for handling files and directories. Key classes include:
  • File: Provides static methods for creating, copying, deleting, moving, and opening files.
  • FileInfo: Provides instance methods for creating, copying, deleting, moving, and opening files, and also for retrieving file properties.
  • Directory: Provides static methods for creating, moving, and enumerating through directories and subdirectories.
  • DirectoryInfo: Provides instance methods for creating, moving, and enumerating through directories and subdirectories.

Reading from a File

To read text from a file, you can use the File.ReadAllText, File.ReadAllLines, or StreamReader class.

Using File.ReadAllText

string content = File.ReadAllText(@"C:\path\to\your\file.txt"); Console.WriteLine(content);

Using File.ReadAllLines

string[] lines = File.ReadAllLines(@"C:\path\to\your\file.txt"); foreach (var line in lines) { Console.WriteLine(line); }

Using StreamReader

using (StreamReader reader = new StreamReader(@"C:\path\to\your\file.txt")) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } }

Writing to a File

To write text to a file, you can use the File.WriteAllText, File.WriteAllLines, or StreamWriter class.

Using File.WriteAllText

File.WriteAllText(@"C:\path\to\your\file.txt", "Hello, World!");

Using File.WriteAllLines

string[] lines = { "First line", "Second line" }; File.WriteAllLines(@"C:\path\to\your\file.txt", lines);

Using StreamWriter

using (StreamWriter writer = new StreamWriter(@"C:\path\to\your\file.txt")) { writer.WriteLine("Hello, World!"); }

Working with Directories

Creating, deleting, and enumerating through directories can be done using the Directory or DirectoryInfo class.

Creating a Directory

Directory.CreateDirectory(@"C:\path\to\your\directory");

Deleting a Directory

Directory.Delete(@"C:\path\to\your\directory", true); // True to remove directories, subdirectories, and files.

Enumerating Files in a Directory

foreach (string file in Directory.EnumerateFiles(@"C:\path\to\your\directory")) { Console.WriteLine(file); }

Best Practices for File Handling

  • Always use using statements with StreamReader and StreamWriter to ensure that file handles are properly disposed.
  • Handle exceptions, such as IOException, to manage errors like file not found, permission issues, or file in use scenarios.
  • When dealing with large files, prefer streaming (StreamReader/StreamWriter) over loading the entire file into memory (File.ReadAllText/File.WriteAllText).

Summary

File handling is a critical component of many C# applications, enabling direct interaction with the filesystem. By leveraging the System.IO namespace, you can perform a wide range of file operations efficiently. Understanding how to read from and write to files, manage directories, and implement best practices for file I/O operations are essential skills for any C# developer.