C# - How to Move files from a folder to Another Folder in C Sharp

Scenario: Download Script

You are working as C# developer, you need to create a program that should move the files from source folder to destination folder. 

Below code can be used to move all the files from a folder to another folder.

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 Move to destination folder
          foreach (FileInfo file in files)
            {

              file.MoveTo(DestinationFolder+file.Name);

            }

        }
    }
}

No comments:

Post a Comment