[C#] Копирование содержимого каталогов вместе с подкаталогам
От: VladD2 Российская Империя www.nemerle.org
Дата: 08.05.06 17:20
Оценка: 13 (2)
Собственно объяснять думаю нечего.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Rsdn.Utils
{
    enum Overwrite
    {
        No,
        Older,
        All,
    }

    static class Utils
    {
        public static void CopyDirs(string sorseRoot, string destRoot, Overwrite overwrite)
        {
            foreach (string dir in Directory.GetDirectories(
                    sorseRoot, "*.*", SearchOption.AllDirectories))
            {
                string dirName = Path.GetFileName(dir);
                string destSubDor = Path.Combine(destRoot, dirName);
                Directory.CreateDirectory(destSubDor);
                string sorseSubDor = Path.Combine(sorseRoot, dirName);
                CopyDirs(sorseSubDor, destSubDor, overwrite);
            }

            foreach (string filePath in Directory.GetFiles(sorseRoot))
            {
                string fileName = Path.GetFileName(filePath);
                string destPath = Path.Combine(destRoot, fileName);
                bool exists = File.Exists(destPath);

                if (exists)
                {
                    switch (overwrite)
                    {
                        case Overwrite.Older:
                            if (new FileInfo(filePath).LastWriteTimeUtc 
                                > new FileInfo(destPath).LastWriteTimeUtc)
                            {
                                File.Copy(filePath, destPath, true);
                            }
                            break;
                        case Overwrite.All:
                            File.Copy(filePath, destPath, true);
                            break;
                    }
                }
                else
                    File.Copy(filePath, destPath, true);
            }
        }
    }
}
... << RSDN@Home 1.2.0 alpha rev. 637>>
Есть логика намерений и логика обстоятельств, последняя всегда сильнее.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.