C# how to copy all files in a folder

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace CopyDir
{
    class Program
    {
        static void Main(string[] args)
        {
            string FromDir = @"c:\source";
            string ToDir = @"c:\destination";
            if (!Directory.Exists(ToDir))
 
                Directory.CreateDirectory(ToDir);
 
            string[] Files;
            Files = Directory.GetFileSystemEntries(FromDir);
 
            for (int k = 0; k < Files.Length; k++)
            {
                string FromFile = Path.GetFileName(Files[k]);
 
                FileAttributes FileAttr = File.GetAttributes(Files[k]);
 
                if ((FileAttr & FileAttributes.Directory)== FileAttributes.Directory)
                    continue;
 
                Console.WriteLine(FromFile);
 
        string arrival = ToDir + "\\" + FromFile;
        File.Copy(Files[k], arrival, true);
                 
            }
            Console.ReadLine();
         }
    }
}

No comments:

Post a Comment