Home » C# programs » C# file handling programs

C# program to copy content of one file to another file

In this C# program, we are going to learn how to copy content of one file to another file? To implement this program we are using File.Copy() method.
Submitted by IncludeHelp, on November 02, 2017

Given a file and we have to copy its content to another file using C# program.

File.Copy()

This is a method of "File class, which is used to copy all data of source file to the destination file.

Syntax:

File.Copy(source_file,dest_file);

Parameter(s):

  1. source_file - From which we are copying data content.
  2. dest_file - In which data is being copied.

Program

using System;
using System.IO;

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

            data = File.ReadAllText("ABC.TXT");
            Console.WriteLine("Content of ABC.TXT :\n"+data);

            File.Copy("ABC.TXT", "XYZ.TXT");

            data = File.ReadAllText("XYZ.TXT");
            Console.WriteLine("Content of XYZ.TXT :\n" + data);

        }
    }
}

Output

Content of ABC.TXT :
India is a great country.
Content of XYZ.TXT :
India is a great country.

Note: In above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

In above function, overwriting a file with the same name is not allowed.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.