Home Programs Copy the Contents from one File to another File in C#

Copy the Contents from one File to another File in C#

In this example we show copy the contents of one file into another in C#

Example

This example will take a file called testfile.txt and copy the contents to a new file called newfile.txt.

We use the File.Copy method to copy the contents of one file to another.

We then read the contents of the files and display the output to the user

using System;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            File.Copy("testfile.txt", "newfile.txt");
            Console.WriteLine(File.ReadAllText("testfile.txt"));
            Console.WriteLine(File.ReadAllText("newfile.txt"));
            Console.Read();
        }
    }
}

This will display something like this with the test files that I used

This is my test file with some test text in it
This is my test file with some test text in it

You may also like