Q:

C# program to copy content of one file to another file by overwriting same file name

0

Given a file and we have to copy its content to another file by overwriting same file name 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, here we are using an extra parameter to over write the file.

Syntax:

File.Copy(source_file,dest_file,overWrting);

Parameter(s):

  1. source_file - From which we are copying data content.
  2. dest_file - In which data is being copied.
  3. overWrting - It is a Boolean flag, true allowed overwriting.
  4.  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program

using System;
using System.IO;

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

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

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

            File.Copy("source/ABC.TXT", "dest/ABC.TXT",true);

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

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

        }
    }
}

Output

Content Before copy:

Content of source/ABC.TXT :
India is a great country.
Content of dest/ABC.TXT :
Think big, Think beyond.


Content After copy:

Content of source/ABC.TXT :
India is a great country.
Content of dest/ABC.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.

Here program is copying the content of 'source/ABC.TXT' to 'dest/ABC.TXT' and In above Copy method there is a third Boolean parameter is used to allow/deny overwriting content in case of same file name.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# file handling (File class) solved programs/examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to get last access time of file or dire... >>
<< C# program to copy content of one file to another ...