class contentSearch
{
private DirectoryInfo _baseDirectory = null;
public DirectoryInfo BaseDirectory
{
get { return _baseDirectory; }
set { _baseDirectory = value; }
}
public contentSearch(string baseDirectory)
{
this._baseDirectory = new DirectoryInfo(baseDirectory);
}
/// <summary>
/// 递归匹配包含指定内容的文件全名
/// </summary>
/// <param name="dir"></param>
/// <param name="content"></param>
/// <param name="reval"></param>
public void GetFiles(DirectoryInfo dir, string content, ref List<string> reval)
{
if (dir.Exists)
{
foreach (FileInfo f in dir.GetFiles())
{
if (IsIncludeFile(f))
{
using (StreamReader sr = File.OpenText(f.FullName))
{
String input;
while ((input = sr.ReadLine()) != null)
{
// Console.WriteLine(input);
if (this.IsExistsContent(input.ToLower(), content))
{
reval.Add(f.FullName);
break;
}
}
Console.WriteLine("The end of the stream has been reached.");
sr.Close();
}
}
}
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
this.GetFiles(subDir, content, ref reval);
}
}
}
/// <summary>
/// 是否是不包括的扩展名
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
private bool IsExcludeFile(FileInfo f)
{
bool reval = false;
string[] excludeFileType = new string[] { ".exe", ".bak" };
foreach (string s in excludeFileType)
{
if (f.Extension.ToLower() == s)
{
reval = true;
break;
}
}
return reval;
}
/// <summary>
/// 是否是包括的扩展名
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
private bool IsIncludeFile(FileInfo f)
{
bool reval = false;
string[] excludeFileType = new string[] { ".asp", ".aspx", ".cs" };
foreach (string s in excludeFileType)
{
if (f.Extension.ToLower() == s)
{
reval = true;
break;
}
}
return reval;
}
/// <summary>
/// 读取的信息是否存在给定的内容中的一个,存在返回true
/// </summary>
/// <param name="sourceString"></param>
/// <param name="content"></param>
/// <returns></returns>
private bool IsExistsContent(string sourceString, string content)
{
bool reval = false;
foreach (string s in content.Split(','))
{
if (sourceString.Contains(s))
{
reval = true;
break;
}
}
return reval;
}
/// <summary>
/// 根据源list中的文件和查询关键字列表,输出文件包含的关键字
/// </summary>
/// <param name="filelist"></param>
/// <param name="keywords"></param>
/// <returns><code>List<文件名:关键字1,关键字2...></code></returns>
public List<string> GetContentsOperation(List<string> filelist, string keywords)
{
List<string> reval = new List<string>();
foreach (string file in filelist)
{
string original = file + ": ";
string revalFile = original;
StreamReader sr = File.OpenText(file);
string input;
while ((input = sr.ReadLine()) != null)
{
string revalkewords=Getkeywords(input.ToLower(), keywords);
if (!string.IsNullOrEmpty(revalkewords))
{
revalFile = revalFile == original ? revalFile + revalkewords : revalFile + "," + revalkewords;
}
}
reval.Add(revalFile);
}
return reval;
}
/// <summary>
/// 读取源字符串中包含关键字,包含多项以逗号分隔
/// </summary>
/// <param name="sourceString"></param>
/// <param name="keywords"></param>
/// <returns>关键字1,关键字2...</returns>
private string Getkeywords(string sourceString, string keywords)
{
string reval = string.Empty;
foreach (string s in keywords.Split(','))
{
if (sourceString.Contains(s))
{
reval = reval == string.Empty ? s : reval + "," + s;
}
}
return reval;
}
}