Advertisement
LostProphet

FileSearching

Dec 4th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace ReplaceText
  8. {
  9.     class Program
  10.     {
  11.         private static int lineNumber = 1;
  12.         private static List<FileInfo> files = new List<FileInfo>();
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Write("Directory to search: ");
  17.             DirectoryInfo directory = new DirectoryInfo(Console.ReadLine());
  18.  
  19.             Console.Write("Type of file to search: ");
  20.             string typeToFind = Console.ReadLine();
  21.  
  22.             Console.Write("Text to find: ");
  23.             string textToFind = Console.ReadLine();
  24.  
  25.             foreach (FileInfo file in directory.GetFiles(typeToFind))
  26.             {
  27.                 PrintColourLine("Searching " + file.Name + "...", ConsoleColor.Cyan);
  28.  
  29.                 using (StreamReader reader = new StreamReader(new FileStream(file.FullName, FileMode.Open)))
  30.                 {
  31.                     foreach (string line in reader.ReadToEnd().Split("\n".ToCharArray()))
  32.                     {
  33.                         if (line.Contains(textToFind))
  34.                         {
  35.                             if (!files.Contains(file))
  36.                                 files.Add(file);
  37.  
  38.                             PrintColourLine(string.Format("Found {0} in {1} on line {2}!", textToFind, file.Name, lineNumber), ConsoleColor.Green);
  39.  
  40.                             //Console.Write(line);
  41.                             //Console.ReadLine();
  42.  
  43.                             //return;
  44.                         }
  45.  
  46.                         lineNumber++;
  47.                     }
  48.  
  49.                     lineNumber = 1;
  50.                 }
  51.  
  52.                 if (!files.Contains(file))
  53.                     PrintColourLine(textToFind + " not found!", ConsoleColor.Red);
  54.  
  55.                 Console.ReadLine();
  56.             }
  57.         }
  58.  
  59.         private static void PrintColour(string message, ConsoleColor foreColour, ConsoleColor backColour = ConsoleColor.Black)
  60.         {
  61.             ConsoleColor fore = Console.ForegroundColor, back = Console.BackgroundColor;
  62.  
  63.             Console.ForegroundColor = foreColour;
  64.             Console.BackgroundColor = backColour;
  65.  
  66.             Console.Write(message);
  67.  
  68.             Console.ForegroundColor = fore;
  69.             Console.BackgroundColor = back;
  70.         }
  71.  
  72.         private static void PrintColourLine(string message, ConsoleColor foreColour, ConsoleColor backColour = ConsoleColor.Black)
  73.         {
  74.             PrintColour(message + Environment.NewLine, foreColour, backColour);
  75.         }
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement