Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

File Operations in C# – A Practical Guide

File Operations in C# – A Practical Guide

In C# and .NET, working with files is a common requirement. Whether you need to verify a file’s presence, read its contents, or manage file copies, the System.IO.File class offers straightforward, thread‑safe methods. Below you’ll find concise explanations and sample code that follows best practices and Microsoft’s official guidance.

Basic File Actions

  1. Reading – Retrieve data from an existing file.
  2. Writing – Overwrite the entire file with new content (the default behavior).
  3. Appending – Add data to the end of a file without deleting existing content.

Key Methods Covered

Prerequisites

For all examples we assume a text file D:\Example.txt exists with the following two lines:

Guru99 – .Net
Guru99 – C#

1. Checking File Existence – File.Exists

using System;
using System.IO;

namespace DemoApplication
{
    class Tutorial
    {
        static void Main(string[] args)
        {
            string path = "D:\\Example.txt";

            if (File.Exists(path))
            {
                Console.WriteLine("File Exists");
            }
            else
            {
                Console.WriteLine("File Not Found");
            }
            Console.ReadKey();
        }
    }
}

The program outputs File Exists when D:\Example.txt is present.

2. Reading Lines Individually – File.ReadAllLines

using System;
using System.IO;

namespace DemoApplication
{
    class Tutorial
    {
        static void Main(string[] args)
        {
            string path = "D:\\Example.txt";
            string[] lines = File.ReadAllLines(path);

            foreach (var line in lines)
            {
                Console.WriteLine(line);
            }
            Console.ReadKey();
        }
    }
}

Each line from the file is printed on a separate console line.

3. Reading the Entire File – File.ReadAllText

using System;
using System.IO;

namespace DemoApplication
{
    class Tutorial
    {
        static void Main(string[] args)
        {
            string path = "D:\\Example.txt";
            string content = File.ReadAllText(path);
            Console.WriteLine(content);
            Console.ReadKey();
        }
    }
}

The console displays both lines concatenated with a line break.

4. Copying a File – File.Copy

using System;
using System.IO;

namespace DemoApplication
{
    class Tutorial
    {
        static void Main(string[] args)
        {
            string source = "D:\\Example.txt";
            string destination = "D:\\ExampleNew.txt";

            File.Copy(source, destination, overwrite: true);
            Console.WriteLine("Copy succeeded");
            Console.ReadKey();
        }
    }
}

The new file D:\ExampleNew.txt contains an exact copy of Example.txt.

5. Deleting a File – File.Delete

using System;
using System.IO;

namespace DemoApplication
{
    class Tutorial
    {
        static void Main(string[] args)
        {
            string path = "D:\\Example.txt";
            File.Delete(path);
            Console.WriteLine("File deleted");
            Console.ReadKey();
        }
    }
}

After execution, D:\Example.txt is permanently removed from the D drive.

Summary

C Language

  1. Mastering C Input and Output (I/O): scanf() and printf() Explained
  2. C File Handling: Mastering I/O with fopen, fprintf, fread, and fseek
  3. Python File I/O: Mastering File Operations, Reading, Writing, and Management
  4. Java Input/Output Streams: Fundamentals and Types
  5. Process Automation I/O Systems: Market Trends, Decentralization, and Ethernet‑Based Field Connectivity
  6. Mastering C File I/O: Create, Open, Read, Write, and Close Files with Confidence
  7. Mastering File I/O in Verilog: Open, Read, Write, and Close
  8. Mastering File I/O in C: Creating, Opening, and Managing Files
  9. Mastering C# File I/O: Reading, Writing, and Managing Streams
  10. Python File I/O: Mastering Input and Output Operations