using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; using System.Text.RegularExpressions;
namespace System.IO { public static class ExtensionMethods { #region Enumerators public enum Option { SearchContent = 0, SearchTitle = 1, SearchTitleAndContent = 2 } #endregion #region Internal Classes internal class FileInfoComparer : IEqualityComparer<FileInfo> { public bool Equals(FileInfo a, FileInfo b) { return a.FullName == b.FullName; } public int GetHashCode(FileInfo fi) { return fi.GetHashCode(); } } #endregion #region Public Methods public static FileInfo[] GetFiles(this DirectoryInfo DirectoryInfoObject, string searchPattern, Option option, SearchOption searchOption) { List<FileInfo> matchedFiles = new List<FileInfo>(); if (searchOption == SearchOption.AllDirectories) { foreach (DirectoryInfo di in DirectoryInfoObject.GetDirectories()) { matchedFiles.AddRange(di.GetFiles(searchPattern, option, SearchOption.AllDirectories).ToList()); } } FileInfoComparer comparer = new FileInfoComparer(); if (option == Option.SearchContent || option == Option.SearchTitleAndContent) { foreach (FileInfo fi in DirectoryInfoObject.GetFiles()) { StreamReader sr = new StreamReader(fi.FullName); if (Regex.Match(sr.ReadToEnd(), searchPattern).Success) if (!matchedFiles.Contains(fi, comparer)) matchedFiles.Add(fi); } } if (option == Option.SearchTitle || option == Option.SearchTitleAndContent) { foreach (FileInfo fi in DirectoryInfoObject.GetFiles()) { if (Regex.Match(fi.Name, searchPattern).Success) if (!matchedFiles.Contains(fi, comparer)) matchedFiles.Add(fi); } } return matchedFiles.ToArray(); }
public static void CopyTo(this DirectoryInfo DirectoryInfoObject, string DestinationDirectoryPath, bool Overwrite) { if (Overwrite) { Directory.Delete(DestinationDirectoryPath, true); } CopyHelper(DirectoryInfoObject, DestinationDirectoryPath, Overwrite); } public static void FileDiff(this DirectoryInfo dirA, DirectoryInfo dirB, out FileInfo[] inBoth, out FileInfo[] inA, out FileInfo[] inB) { var inDirBoth = from file in dirA.GetFiles().ToList() join file1 in dirB.GetFiles().ToList() on file.Name equals file1.Name select file; var inDirAOnly = from file in dirA.GetFiles().ToList() join file1 in dirB.GetFiles().ToList() on file.Name equals file1.Name into diff from file2 in diff.DefaultIfEmpty() where file2 == null select file; var inDirBOnly = from file in dirB.GetFiles().ToList() join file1 in dirA.GetFiles().ToList() on file.Name equals file1.Name into diff from file2 in diff.DefaultIfEmpty() where file2 == null select file; inBoth = inDirBoth.ToArray(); inA = inDirAOnly.ToArray(); inB = inDirBOnly.ToArray(); } public static int CompareFilesTo(this DirectoryInfo dirA, DirectoryInfo dirB) { var inDirAOnly = from file in dirA.GetFiles().ToList() join file1 in dirB.GetFiles().ToList() on file.Name equals file1.Name into diff from file2 in diff.DefaultIfEmpty() where file2 == null select file; var inDirBOnly = from file in dirB.GetFiles().ToList() join file1 in dirA.GetFiles().ToList() on file.Name equals file1.Name into diff from file2 in diff.DefaultIfEmpty() where file2 == null select file; if (inDirAOnly.Count() > inDirBOnly.Count()) return 1; if (inDirAOnly.Count() < inDirBOnly.Count()) return -1; return 0; } public static DirectoryInfo[] GetDirectories(this DirectoryInfo di, IComparer<DirectoryInfo> comparer) { List<DirectoryInfo> lstDirectories = new List<DirectoryInfo>(di.GetDirectories()); lstDirectories.Sort(comparer); return lstDirectories.ToArray(); } public static DirectoryInfo[] GetDirectories(this DirectoryInfo di, Comparison<DirectoryInfo> comparer) { List<DirectoryInfo> lstDirectories = new List<DirectoryInfo>(di.GetDirectories()); lstDirectories.Sort(comparer); return lstDirectories.ToArray(); } #endregion #region Private Methods private static void CopyHelper(this DirectoryInfo DirectoryInfoObject, string DestinationDirectoryPath, bool Overwrite) { if (!Directory.Exists(DestinationDirectoryPath)) Directory.CreateDirectory(DestinationDirectoryPath); foreach (string directory in Directory.GetDirectories(DirectoryInfoObject.FullName)) { DirectoryInfo di = new DirectoryInfo(directory); di.CopyHelper(DestinationDirectoryPath + @"\" + di.Name, Overwrite); } foreach (string file in Directory.GetFiles(DirectoryInfoObject.FullName)) { FileInfo fi = new FileInfo(file); fi.CopyTo(DestinationDirectoryPath + @"\" + fi.Name, Overwrite); } } #endregion } }
|