/// <summary> /// Finds all files in a given set of paths that contain the specified regex /// </summary> /// <param name="regex">The string to search for</param> /// <param name="ignoreCase">True to ignore case</param> /// <param name="paths">Paths to search</param> public static IEnumerable<string> ContainsRegex(this IFileService fileService, string regex, bool ignoreCase, params string[] paths) { RegexOptions options = RegexOptions.Compiled; if (ignoreCase) { options |= RegexOptions.IgnoreCase; } Regex r; try { r = new Regex(regex, options); } catch (ArgumentException exception) { // Bad regex throw new TaskArgumentException(exception.Message); } ConcurrentBag<string> matchingPaths = new ConcurrentBag<string>(); Parallel.ForEach(paths, path => { if (!fileService.FileExists(path)) { return; } Stream stream = fileService.CreateFileStream(path); using (StreamReader reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) { if (r.IsMatch(line)) { matchingPaths.Add(path); break; } } } }); return matchingPaths.OrderBy(s => s).ToList(); }
/// <summary> /// Simple helper to create a writer on an existing file. Dispose the writer when finished. /// </summary> public static TextWriter CreateWriter(this IFileService fileService, string path) { return new StreamWriter(fileService.CreateFileStream(path, FileMode.Append, FileAccess.Write)); }
/// <summary> /// Simple helper to create a reader on an existing file. Dispose the reader when finished. /// </summary> public static TextReader CreateReader(this IFileService fileService, string path) { return new StreamReader(fileService.CreateFileStream(path)); }
/// <summary> /// Simple line counter for a file, no error handling /// </summary> public static int CountLines(this IFileService fileService, string path) { int lineCount = 0; bool trailingCharacter = true; // 8K buffer char[] buffer = new char[8 * 1024]; Stream stream = fileService.CreateFileStream(path); using (StreamReader reader = new StreamReader(stream)) { int read; while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) { for (int i = 0; i < read; i++) { if (buffer[i] == '\n') { trailingCharacter = false; lineCount++; } else { trailingCharacter = true; } } } if (trailingCharacter) { lineCount++; } } return lineCount; }
/// <summary> /// Reads lines from the given path /// </summary> public static IEnumerable<string> ReadLines(this IFileService fileService, string path) { Stream stream = fileService.CreateFileStream(path); using (StreamReader reader = new StreamReader(stream)) { string line = reader.ReadLine(); while (line != null) { yield return line; line = reader.ReadLine(); } } }
/// <summary> /// Returns the MD5 hash for the given file's contents, or null if failed /// </summary> public static byte[] GetHash(this IFileService fileService, string path) { try { // Console.WriteLine("Hashing {0}...", localPath); if (!string.IsNullOrEmpty(path) & fileService.FileExists(path)) { using (Stream fileStream = fileService.CreateFileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read)) { return s_MD5.ComputeHash(fileStream); } } } catch (IOException) { // If there is an I/O problem, do nothing and allow this method to return null. } return null; }