使用 c# 搜索文本文件并显示包含搜索关键字的行号和完整行 [英] search text file using c# and display the line number and the complete line that contains the search keyword
本文介绍了使用 c# 搜索文本文件并显示包含搜索关键字的行号和完整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助来使用 c# 搜索文本文件(日志文件)并显示行号和包含搜索关键字的完整行.
I require help to search a text file (log file) using c# and display the line number and the complete line that contains the search keyword.
推荐答案
这是来自:http://msdn.microsoft.com/en-us/library/aa287535%28VS.71%29.aspx
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\test.txt");
while((line = file.ReadLine()) != null)
{
if ( line.Contains("word") )
{
Console.WriteLine (counter.ToString() + ": " + line);
}
counter++;
}
file.Close();
这篇关于使用 c# 搜索文本文件并显示包含搜索关键字的行号和完整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文