Examples of C # deep copy folders

Source: Internet
Author: User
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 {Pub            Lic 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 Direct            Oryinfo (@ "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 ("Copy succeeded ...");        Console.readkey (); }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.