A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 9.1 File I/O - Reading from and writing to files

 Here's a basic tutorial on File I/O (Input/Output) in C# for reading from and writing to files. File I/O is an essential part of many applications for tasks such as storing and retrieving data, configuration settings, and more.

Reading from Files:

Step 1: Import Necessary Namespace

using System;
using System.IO;

Step 2: Reading Text from a File

class Program
{
    static void Main()
    {
        // Specify the path to the file
        string filePath = "path/to/your/file.txt";

        // Check if the file exists
        if (File.Exists(filePath))
        {
            // Read all lines from the file into an array
            string[] lines = File.ReadAllLines(filePath);

            // Display the content of the file
            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }
        }
        else
        {
            Console.WriteLine("File not found");
        }
    }
}

Step 3: Reading Binary Data from a File

class Program
{
    static void Main()
    {
        string filePath = "path/to/your/file.bin";

        if (File.Exists(filePath))
        {
            // Read all bytes from the file
            byte[] data = File.ReadAllBytes(filePath);

            // Process the binary data as needed
            // (e.g., convert to text, parse as specific format)
        }
        else
        {
            Console.WriteLine("File not found");
        }
    }
}

Writing to Files:

Step 1: Import Necessary Namespace

using System;
using System.IO;

Step 2: Writing Text to a File

class Program
{
    static void Main()
    {
        string filePath = "path/to/your/output.txt";

        // Content to be written to the file
        string content = "Hello, File I/O in C#!";

        // Write the content to the file
        File.WriteAllText(filePath, content);

        Console.WriteLine("File written successfully");
    }
}

Step 3: Writing Binary Data to a File

class Program
{
    static void Main()
    {
        string filePath = "path/to/your/output.bin";

        // Binary data to be written to the file
        byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F }; // ASCII values for "Hello"

        // Write the binary data to the file
        File.WriteAllBytes(filePath, data);

        Console.WriteLine("File written successfully");
    }
}

These examples cover basic file operations in C#. You can customize them based on your specific needs and handle exceptions to enhance the robustness of your file I/O operations.