Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace ReplaceText
- {
- class Program
- {
- private static int lineNumber = 1;
- private static List<FileInfo> files = new List<FileInfo>();
- static void Main(string[] args)
- {
- Console.Write("Directory to search: ");
- DirectoryInfo directory = new DirectoryInfo(Console.ReadLine());
- Console.Write("Type of file to search: ");
- string typeToFind = Console.ReadLine();
- Console.Write("Text to find: ");
- string textToFind = Console.ReadLine();
- foreach (FileInfo file in directory.GetFiles(typeToFind))
- {
- PrintColourLine("Searching " + file.Name + "...", ConsoleColor.Cyan);
- using (StreamReader reader = new StreamReader(new FileStream(file.FullName, FileMode.Open)))
- {
- foreach (string line in reader.ReadToEnd().Split("\n".ToCharArray()))
- {
- if (line.Contains(textToFind))
- {
- if (!files.Contains(file))
- files.Add(file);
- PrintColourLine(string.Format("Found {0} in {1} on line {2}!", textToFind, file.Name, lineNumber), ConsoleColor.Green);
- //Console.Write(line);
- //Console.ReadLine();
- //return;
- }
- lineNumber++;
- }
- lineNumber = 1;
- }
- if (!files.Contains(file))
- PrintColourLine(textToFind + " not found!", ConsoleColor.Red);
- Console.ReadLine();
- }
- }
- private static void PrintColour(string message, ConsoleColor foreColour, ConsoleColor backColour = ConsoleColor.Black)
- {
- ConsoleColor fore = Console.ForegroundColor, back = Console.BackgroundColor;
- Console.ForegroundColor = foreColour;
- Console.BackgroundColor = backColour;
- Console.Write(message);
- Console.ForegroundColor = fore;
- Console.BackgroundColor = back;
- }
- private static void PrintColourLine(string message, ConsoleColor foreColour, ConsoleColor backColour = ConsoleColor.Black)
- {
- PrintColour(message + Environment.NewLine, foreColour, backColour);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement