這裏是一個可能的方式做到這一點:
StreamReader sr;
string fileContents;
string[] logFiles = Directory.GetFiles(@"C:\Logs");
foreach (string file in logFiles)
{
using (StreamReader sr = new StreamReader(file))
{
fileContents = sr.ReadAllText();
if (fileContents.Contains("ACTION:") || fileContents.Contains("INPUT:") || fileContents.Contains("RESULT:"))
{
// Do what you need to here
}
}
}
您可能需要根據您的確切實施需求做一些變化 - 例如,如果該詞跨越兩行,該行是否需要以該詞開頭,那麼該怎麼辦等。
新增
備用線路逐線檢查:
StreamReader sr;
string[] lines;
string[] logFiles = Directory.GetFiles(@"C:\Logs");
foreach (string file in logFiles)
{
using (StreamReader sr = new StreamReader(file)
{
lines = sr.ReadAllLines();
foreach (string line in lines)
{
if (line.Contains("ACTION:") || line.Contains("INPUT:") || line.Contains("RESULT:"))
{
// Do what you need to here
}
}
}
}
來源
2011-08-25 00:03:24
Tim
10 KB真的很小。我已經使用過大的文件以適應內存=/ – NullUserException