C# copy folder and files from source path to target path

static void Main(string[] args)
        {
            string sourceDir = @"E:\SourcePath";
            string destDir = @"E:\Dest";
            CopyDirectoriesFiles(sourceDir, destDir);
            Console.ReadLine();       
        }

        public static void CopyDirectoriesFiles(string sourceDirectory, string targetDirectory)
        {
            var diSource = new DirectoryInfo(sourceDirectory);
            var diTarget = new DirectoryInfo(targetDirectory);
            CopyAll(diSource, diTarget);
        }

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            Directory.CreateDirectory(target.FullName);

            // Copy each file into the new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
                fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
            }

            // Copy each subdirectory using recursion.
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }

 

C# copy folder and files from source path to target path

上一篇:Android Google官方文档(cn)解析之——Intents and Intent filter


下一篇:Winform中控制文本框输入的值(只显示允许输入类型的值)