C# - How to copy all files in a folder to another folder in C Sharp

Scenario: Download Script

You are working as C# developer and you need to create a Console Application, that should read all the files from a folder and copy to another folder in C#.

The below script can be used to copy all the files from a folder to new folder in C Sharp.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TechBrothersIT.com_CSharp_Tutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            //Provider Source Folder Path
            string SourceFolder = @"C:\Source\";
            //Provide Destination Folder path
            string DestinationFolder = @"C:\Destination\";
            
           var files = new DirectoryInfo(SourceFolder).GetFiles("*.*");
      
            //Loop throught files and copy to destination folder
          foreach (FileInfo file in files)
            {
                file.CopyTo(DestinationFolder + file.Name);
            }
            
        }
    }
}

No comments:

Post a Comment