Finds text in the all files in the given directory:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
public class Finder
{
public static FindObject[] FindText(string directory_path, string text)
{
string [] files = Directory.GetFiles(directory_path);
ListfindObjList = new List ();
try
{
foreach (string fName in files)
{
StreamReader testTxt = new StreamReader(fName);
string allRead = testTxt.ReadToEnd();//Reads the whole text file to the end
testTxt.Close(); //Closes the text file after it is fully read.
string regMatch = text;//string to search for inside of text file. It is case sensitive.
if (Regex.IsMatch(allRead, regMatch))//If the match is found in allRead
{
FindObject findObj = new FindObject();
findObj.success = true;
findObj.filename = fName;
findObjList.Add(findObj);
}
}
}
catch (Exception err)
{
Console.Out.WriteLine(err.Message);
}
return findObjList.ToArray();
}
}
public class FindObject
{
public string filename;
public bool success;
}
No comments:
Post a Comment