Copy All files in a folder - CSharp System.IO

CSharp examples for System.IO:File Copy

Description

Copy All files in a folder

Demo Code


using System.IO;/*w ww.  ja va 2s .  c  om*/
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static void CopyAll(string source, string target)
        {
            EnsureDirectoryExists(target);

            foreach (var file in new DirectoryInfo(source).GetFiles())
            {
                file.CopyTo(Path.Combine(target, file.Name), true);
            }

            foreach (DirectoryInfo sourceSubdirectory in new DirectoryInfo(source).GetDirectories())
            {
                DirectoryInfo targetSubdirectory = Directory.CreateDirectory(Path.Combine(target, sourceSubdirectory.Name));
                CopyAll(sourceSubdirectory.FullName, targetSubdirectory.FullName);
            }
        }
}

Related Tutorials