What is Files all type with Example Code

In C#, you can work with different types of files using various classes provided by
the .NET Framework.



using System;

using System.IO;

 class Program

{

    static void Main(string[] args)

    {

        string filePath = "example.txt";

        // Check if the file exists

        if (File.Exists(filePath))

        {

            // Read the content of the file

            string fileContent = File.ReadAllText(filePath);

            Console.WriteLine("Content of the file:");

            Console.WriteLine(fileContent);

 

            // Append text to the file

            File.AppendAllText(filePath, "\nThis line is appended.");

 

            // Read the modified content of the file

            fileContent = File.ReadAllText(filePath);

            Console.WriteLine("\nContent after appending text:");

            Console.WriteLine(fileContent);

 

            // Replace content of the file

            File.WriteAllText(filePath, "This is the new content.");

 

            // Read the modified content of the file

            fileContent = File.ReadAllText(filePath);

            Console.WriteLine("\nContent after replacing text:");

            Console.WriteLine(fileContent);

 

            // Create a copy of the file

            string copyPath = "example_copy.txt";

            File.Copy(filePath, copyPath, true);

            Console.WriteLine($"\nFile copied to: {copyPath}");

 

            // Delete the original file

            File.Delete(filePath);

            Console.WriteLine("\nOriginal file deleted.");

 

            // Check if the copied file exists

            if (File.Exists(copyPath))

            {

                // Read the content of the copied file

                fileContent = File.ReadAllText(copyPath);

                Console.WriteLine("\nContent of the copied file:");

                Console.WriteLine(fileContent);

 

                // Replace content of the copied file

                File.WriteAllText(copyPath, "New content in the copied file.");

 

                // Read the modified content of the copied file

                fileContent = File.ReadAllText(copyPath);

                Console.WriteLine("\nContent of the copied file after replacing text:");

                Console.WriteLine(fileContent);

            }

            else

            {

                Console.WriteLine("\nCopied file does not exist.");

            }

        }

        else

        {

            Console.WriteLine("File does not exist.");

        }

    }

}



using System;

using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        // Write text to a text file

        File.WriteAllText("example.txt", "Hello, world!");

 

        // Read text from a text file

        string text = File.ReadAllText("example.txt");

        Console.WriteLine("Text read from file: " + text);

    }

}

 

using System;

using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        // Write binary data to a file

        byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F }; // ASCII values for "Hello"

        File.WriteAllBytes("example.bin", data);

 

        // Read binary data from a file

        byte[] readData = File.ReadAllBytes("example.bin");

        Console.Write("Binary data read from file: ");

        foreach (byte b in readData)

        {

            Console.Write((char)b); // Convert bytes to characters

        }

        Console.WriteLine();

    }

}



using System;

using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        // Write CSV data to a file

        string csvData = "Name, Age\nA, 28\nB, 24";

        File.WriteAllText("example.csv", csvData);

 

        // Read CSV data from a file

        string[] lines = File.ReadAllLines("example.csv");

        Console.WriteLine("CSV data read from file:");

        foreach (string line in lines)

        {

            Console.WriteLine(line);

        }

    }

}

 

using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        string filePath = "newfile.txt";

 

        // Create a new file

        using (FileStream fs = File.Create(filePath))

        {

            // Perform any additional operations if needed

        }

    }

}


using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        string filePath = "fileToDelete.txt";

 

        // Delete a file

        File.Delete(filePath);

    }

}



using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        string sourceFile = "source.txt";

        string destinationFile = "destination.txt";

 

        // Replace the contents of the destination file with the contents of the source file

        File.Replace(sourceFile, destinationFile, null);

    }

}



using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        string filePath = "example.txt";

        string fileContent = "This is some text to write to the file.";

 

        // Write text to a new file

        File.WriteAllText(filePath, fileContent);

    }

}



using System.IO;

 

class Program

{

    static void Main(string[] args)

    {

        string sourceFile = "source.txt";

        string destinationFile = "destination.txt";

 

        // Replace the contents of the destination file with the contents of the source file

        File.Replace(sourceFile, destinationFile, null);

    }

}