Copy Folders in C# using System.IO

后端 未结 9 889
傲寒
傲寒 2020-12-10 03:20

I need to Copy folder C:\\FromFolder to C:\\ToFolder

Below is code that will CUT my FromFolder and then will create my ToFolder. So my FromFolder will be gone and al

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 04:08

    A simple function that copies the entire contents of the source folder to the destination folder and creates the destination folder if it doesn't exist

    class Utils
    {
        internal static void copy_dir(string source, string dest)
        {
            if (String.IsNullOrEmpty(source) || String.IsNullOrEmpty(dest)) return;
            Directory.CreateDirectory(dest);
            foreach (string fn in Directory.GetFiles(source))
            {
                File.Copy(fn, Path.Combine(dest, Path.GetFileName(fn)), true);
            }
            foreach (string dir_fn in Directory.GetDirectories(source))
            {
                copy_dir(dir_fn, Path.Combine(dest, Path.GetFileName(dir_fn)));
            }
        }
    }
    

提交回复
热议问题