如何使用c#.net。
搜索logfile中的关键字我有一个日志文件,其中有一些信息,前面有关键字,如ipaddress,userid等,我需要解析日志文件并获取数据并在网格视图中显示。
c#
中的任何建议谢谢你, jagadeesh kumar。
答案 0 :(得分:0)
我不知道文件的格式是什么,但我建议您使用StreamReader
来读取文件,然后将其拆分为\n
,然后处理每一行。
这将看起来像这样(未经测试):
string wholeFile = "";
using(StreamReader str = new StreamReader(path))
{
wholeFile = str.ReadToEnd();
}
string[] lines = wholeFile.Split('\n').Replace("\r", "");
for(int i = 0; i < lines.Length; i++)
{
//parse the line
string line = lines[i];
if(line.Trim().StartsWith("ipaddress"))
{
string value = line.Trim().Replace("ipaddress", "");
//Do something with the value here...
}
}
您还可以考虑使用RegExp来解析文件甚至每行。
祝你好运,Alex
答案 1 :(得分:0)
使用阅读器阅读文件,然后在每一行上使用正则表达式找到你想要的内容..
var reader = new StreamReader("path/to/file");
string line;
while ((line = reader.ReadLine()) != null)
{
//parse the line here.
}
请参阅tutorial about regular expressions from Microsoft。如果没有看到文件的格式,就无法给出更具体的答案。