C# program to replace the content of one file from other file and create a backup file

Program

using System;

//We need to include this namespace for file handling.
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string s;

            Console.WriteLine("Content Before Replace:\n\n");
            Console.WriteLine("Content of File(ABC.TXT):");
            s = File.ReadAllText("ABC.TXT");
            Console.WriteLine(s);

            Console.WriteLine("Content of File(123.TXT):");
            s = File.ReadAllText("123.TXT");
            Console.WriteLine(s);

            File.Replace("ABC.TXT", "123.TXT", "XYZ.TXT");

            Console.WriteLine("\n\nContent After Replace:\n\n");
            Console.WriteLine("Content of File(123.TXT):");
            s = File.ReadAllText("123.TXT");
            Console.WriteLine(s);

            Console.WriteLine("Content of File(XYZ.TXT):");
            s = File.ReadAllText("XYZ.TXT");
            Console.WriteLine(s);

        }
    }
}

Output

Content Before Replace:


Content of File(ABC.TXT):
Mahenda Singh Dhoni is a greatest captain of indian cricket team.
Content of File(123.TXT):
Hello , This is a sample program for writing text into file.It is new Text.


Content After Replace:


Content of File(123.TXT):
Mahenda Singh Dhoni is a greatest captain of indian cricket team.
Content of File(XYZ.TXT):
Hello , This is a sample program for writing text into file.It is new Text.

Leave a Comment

error: Alert: Content is protected!!