CSharp examples for File IO:Directory
Copy folder with pattern
// Copyright (c) 2014 OSky. All rights reserved. using System.Threading.Tasks; using System.Text; using System.Linq; using System.IO;//ww w . jav a 2 s .c om using System.Collections.Generic; using System; public class Main{ public static void Copy(string sourcePath, string targetPath, string[] searchPatterns = null) { sourcePath.CheckNotNullOrEmpty("sourcePath"); targetPath.CheckNotNullOrEmpty("targetPath"); if (!Directory.Exists(sourcePath)) { throw new DirectoryNotFoundException("??????????????????"); } if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } string[] dirs = Directory.GetDirectories(sourcePath); if (dirs.Length > 0) { foreach (string dir in dirs) { Copy(dir, targetPath + targetPath + dir.Substring(dir.LastIndexOf("\\", StringComparison.Ordinal))); } } if (searchPatterns != null && searchPatterns.Length > 0) { foreach (string searchPattern in searchPatterns) { string[] files = Directory.GetFiles(sourcePath, searchPattern); if (files.Length <= 0) { continue; } foreach (string file in files) { File.Copy(file, targetPath + file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal))); } } } else { string[] files = Directory.GetFiles(sourcePath); if (files.Length <= 0) { return; } foreach (string file in files) { File.Copy(file, targetPath + file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal))); } } } }