2012-04-12 86 views
5

我需要我在实习中建立的程序的帮助。 这个想法是检查用户在任何PC上登录的频率。 当用户登录时,该信息将记录在文本文件中,如此格式。C#搜索文本文件,返回包含单词的所有行

01-01-2011 16:47:10-002481C218B0-WS3092-Chsbe (XP-D790PRO1) 

现在我需要搜索文本文件和(例如)搜索用户Chsbe的所有登录日期的文本文件。

到目前为止我的代码:

private void btnZoek_Click(object sender, EventArgs e) 
     { 
      int counter = 0; string line; 
      // Read the file and display it line by line. 
      System.IO.StreamReader file = new System.IO.StreamReader("c:\\log.txt"); 
      while((line = file.ReadLine()) != null) 
      {  if (line.Contains(txtZoek.Text))  
      { 
       txtResult.Text = line.ToString();     
      }  

      } 
      file.Close(); 
     } 

我的问题是,我该如何返回一个包含搜索关键词,以txtResult日志中的所有字符串?

+1

直视正则表达式 – 2012-04-12 09:16:39

+0

难道possbile获得源为XML? 否则如前所述看看正则表达式。 如果搜索变得更复杂看看某种全文搜索组件 – 2012-04-12 09:18:09

回答

6

您已经做了很好的工作。唯一的错误是写入最后一行读入覆盖前一行的文本框。
你需要使用StringBuilder和using声明在你的一次性流是这样的:

private void btnZoek_Click(object sender, EventArgs e)   
{    
    int counter = 0; string line;    
    StringBuilder sb = new StringBuilder(); 

    // Read the file and display it line by line.    
    using(System.IO.StreamReader file = new System.IO.StreamReader("c:\\log.txt")) 
    { 
     while((line = file.ReadLine()) != null)    
     {  
     if (line.Contains(txtZoek.Text))     
     {   
       // This append the text and a newline into the StringBuilder buffer  
       sb.AppendLine(line.ToString()); 
     }     
     }    
    } 
    txtResult.Text = sb.ToString(); 
} 
当然

,你txtResult应该具备的使用性能多行设置为true,否则你将无法看到的输出。
记住using是更好的方式来处理这种情况,因为它会自动处理也始料未及文件例外照顾,以正确地关闭流

+0

是的!就是这个!感谢您的快速帮助,史蒂夫。 – Ralph 2012-04-12 09:36:19

0

使用的RichTextBox或使用多属性 例如

private void btnZoek_Click(object sender, EventArgs e) 
    { 
     int counter = 0; string line; 
     // Read the file and display it line by line. 
     System.IO.StreamReader file = new System.IO.StreamReader("c:\\log.txt"); 
     while((line = file.ReadLine()) != null) 
     {  if (line.Contains(txtZoek.Text))  
     { 
      richtextbox1.Text += "\n" + line.ToString(); 
      txtresult.Text += "\n" + line.ToString(); 
     }  

     } 
     file.Close(); 
    } 
+0

你的意思是Richtextbox? – basti 2012-04-12 09:20:32

+0

哦,是的,小姐点击 – Likurg 2012-04-12 09:26:22

+0

顺便说一句。我认为这对于踏脚靴来说不是很有帮助。你至少应该实际称呼班级名称。链接到MSDN或显示initalisation .. ...这将解释更多,比那里还将有“为什么”使用richtextbox。 – basti 2012-04-12 09:28:35

0

定义一个列表

列表yourList =新列表();

替换行 txtResult.Text = line.ToString();
by yourList.Add(line);

在列表

“yourList”你拿到了包含用户的所有行

+0

我试过这个,但它不会列出所有的清单..你能澄清一下吗? – Ralph 2012-04-12 12:00:55

+0

你的代码如何? – sebastianmehler 2012-04-13 12:05:33

0

喜欢的东西下面可能会帮助您开始使用正则表达式:

string pattern = "Chsbe"; 
Regex rx = new Regex(pattern, RegexOptions.IgnoreCase); 
MatchCollection mc = rx.Matches(inputText); 

foreach (Match m in mc) 
{ 
    Console.WriteLine(m.Value); 
} 
0
private void btnZoek_Click(object sender, EventArgs e) 
{ 
    int counter = 0; string line; 
    StringBuilder str = new StringBuilder(); 
    // Read the file and display it line by line. 
    System.IO.StreamReader file = new System.IO.StreamReader("c:\\log.txt"); 
    while((line = file.ReadLine()) != null) 
    { 
     if (line.Contains(txtZoek.Text))  
     { 
      str.Append(line.ToString());     
     }  
    } 
    file.Close(); 
} 
0

也许这样的事情会工作。

private void btnZoek_Click(object sender, EventArgs e) 
    { 
     int counter = 0; string line; 
     // Read the file and display it line by line. 
     System.IO.StreamReader file = new System.IO.StreamReader("c:\\log.txt"); 
     while((line = file.ReadLine()) != null) 
     {  if (line.Contains(txtZoek.Text))  
     { 
      txtResult.Text = txtResult.Text + Environment.Newline + line.ToString();     
     }  

     } 
     file.Close(); 
    } 

这将是我的版本:

private void btnZoek_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      int counter = 0; 
      string line; 
      List<String> LinesFound = new List<string>(); 

      // Read the file and display it line by line. 
      System.IO.StreamReader file = new System.IO.StreamReader("c:\\log.txt"); 

      while ((line = file.ReadLine()) != null) 
      { 
       if (line.Contains(txtZoek.Text)) 
       { 
        LinesFound.Add(line); 
       } 

      } 
      file.Close(); 

      foreach (string Line in LinesFound) 
      { 
       txtResult.Text = txtResult.Text + Line + Environment.NewLine; 
      } 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Error in btnZoek_Click"); 
     } 
    } 

如果列表很长,我会用一个StringBuilder来创建一个结果字符串作为性能加速。

0

尝试改变这种行 - >

txtResult.Text = line.ToString(); 

到:

txtResult.Text += line.ToString(); 
相关问题