Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to File Handling in C#

1. What is File Handling?

File handling is an essential part of any programming language. It allows you to create, read, update, and delete files from your storage system. In C#, file handling is performed using System.IO namespace which provides various classes for managing files and directories.

2. File Handling Classes in C#

The main classes used for file handling in C# are:

  • File: Provides static methods for creating, copying, deleting, moving, and opening files.
  • FileInfo: Provides instance methods for creating, copying, deleting, moving, and opening files. It also provides properties to get file information.
  • StreamReader: Used for reading characters from a byte stream in a particular encoding.
  • StreamWriter: Used for writing characters to a stream in a particular encoding.

3. Creating and Writing to a File

The following example demonstrates how to create and write to a file using the StreamWriter class:

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("Welcome to file handling in C#.");
        }
        Console.WriteLine("File created and data written successfully.");
    }
}
                

In this example, a file named "example.txt" is created in the same directory as the executable, and two lines of text are written to it.

4. Reading from a File

The following example demonstrates how to read from a file using the StreamReader class:

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, the "example.txt" file is read line by line and each line is printed to the console.

5. Appending to a File

The following example demonstrates how to append text to an existing file using the StreamWriter class:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = "example.txt";
        using (StreamWriter sw = new StreamWriter(path, true))
        {
            sw.WriteLine("This is an appended line.");
        }
        Console.WriteLine("Data appended successfully.");
    }
}
                

In this example, the "example.txt" file is opened in append mode and a new line of text is added to the end of the file.

6. Deleting a File

The following example demonstrates how to delete a file using the File class:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = "example.txt";
        if (File.Exists(path))
        {
            File.Delete(path);
            Console.WriteLine("File deleted successfully.");
        }
        else
        {
            Console.WriteLine("File does not exist.");
        }
    }
}
                

In this example, the "example.txt" file is deleted if it exists in the directory.

7. FileInfo Class

The FileInfo class provides instance methods for working with files. Here is an example that demonstrates how to use the FileInfo class:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = "example.txt";
        FileInfo fileInfo = new FileInfo(path);
        if (fileInfo.Exists)
        {
            Console.WriteLine("File Name: " + fileInfo.Name);
            Console.WriteLine("File Size: " + fileInfo.Length + " bytes");
            Console.WriteLine("File Extension: " + fileInfo.Extension);
            Console.WriteLine("File Directory: " + fileInfo.DirectoryName);
        }
        else
        {
            Console.WriteLine("File does not exist.");
        }
    }
}
                

In this example, various properties of the "example.txt" file are retrieved and displayed.