How to Copy File in C#/CSharp

To copy a file in C#/CSharp, we make use of File class and its method Copy.  This class resides in System.IO namespace.  Example is given below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.IO;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
//Source of file
string source = @"C:\Users\ADMIN\Desktop\folder1\file.txt";
//Destination Folder
string destination = @"C:\Users\ADMIN\Desktop\folder2\";
//Destination Folder + Filename
destination += Path.GetFileName(source);
//Copy method
File.Copy(source, destination);
}
}
}
using System; using System.IO; namespace Hello_World { class Program { static void Main(string[] args) { //Source of file string source = @"C:\Users\ADMIN\Desktop\folder1\file.txt"; //Destination Folder string destination = @"C:\Users\ADMIN\Desktop\folder2\"; //Destination Folder + Filename destination += Path.GetFileName(source); //Copy method File.Copy(source, destination); } } }
using System;
using System.IO;

namespace Hello_World
{

    class Program
    {
        static void Main(string[] args)
        {
            //Source of file
            string source = @"C:\Users\ADMIN\Desktop\folder1\file.txt";
            
            //Destination Folder
            string destination = @"C:\Users\ADMIN\Desktop\folder2\";

            //Destination Folder + Filename
            destination += Path.GetFileName(source);

            //Copy method
            File.Copy(source, destination);
        }
    }
}