使用c#搜索文本文件,并显示行号和包含搜索关键字的完整行 [英] search text file using c# and display the line number and the complete line that contains the search keyword

查看:811
本文介绍了使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆