程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#深拷貝文件夾示例

c#深拷貝文件夾示例

編輯:C#入門知識

c#深拷貝文件夾示例。本站提示廣大學習愛好者:(c#深拷貝文件夾示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#深拷貝文件夾示例正文



using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FileUtility
{
    public class Program
    {
        public static void DeepCopy(DirectoryInfo source, DirectoryInfo target, params string[] excludePatterns)
        {
            if (target.FullName.Contains(source.FullName))
                return;

            // Go through the Directories and recursively call the DeepCopy Method for each one
            foreach (DirectoryInfo dir in source.GetDirectories())
            {
                var dirName = dir.Name;
                var shouldExclude = excludePatterns.Aggregate(false, (current, pattern) => current || Regex.Match(dirName, pattern).Success);

                if (!shouldExclude)
                    DeepCopy(dir, target.CreateSubdirectory(dir.Name), excludePatterns);
            }

            // Go ahead and copy each file to the target directory
            foreach (FileInfo file in source.GetFiles())
            {
                var fileName = file.Name;
                var shouldExclude = excludePatterns.Aggregate(false,
                                                              (current, pattern) =>
                                                              current || Regex.Match(fileName, pattern).Success);

                if (!shouldExclude)
                    file.CopyTo(Path.Combine(target.FullName, fileName));
            }
        }

        static void Main(string[] args)
        {
            DeepCopy(new DirectoryInfo(@"d:/test/b"), new DirectoryInfo(@"d:/test/a"));
            DeepCopy(new DirectoryInfo(@"d:/test/c"), new DirectoryInfo(@"d:/test/c/c.1"));
            DeepCopy(new DirectoryInfo(@"d:/test/1/"), new DirectoryInfo(@"d:/test/2/"), new string[] { ".*\\.txt" });

            Console.WriteLine("復制勝利...");
            Console.ReadKey();
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved