5

我需要帮助我在实习期间建立的一个项目。这个想法是检查用户在任何 PC 上登录的频率。当用户登录时,该信息会记录在一个文本文件中,就像这种格式。

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?

4

7 回答 7

6

你已经做得很好了。唯一的错误是写入文本框的最后一行会覆盖前一行。
您需要使用 StringBuilder 和using围绕一次性 Stream 的语句,如下所示:

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 应该将属性 MultiLine 设置为 true,否则您将无法看到输出。
请记住,这using是处理这种情况的更好方法,因为它还会自动处理意外的文件异常,注意正确关闭您的 Stream

于 2012-04-12T09:21:50.647 回答
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(); 
    } 
于 2012-04-12T09:18:58.057 回答
0

定义一个列表

列出 yourList = new List();

替换 Line txtResult.Text = line.ToString();
通过 yourList.Add(line);

在列表“yourList”中,你得到了包含用户的所有行

于 2012-04-12T09:20:45.797 回答
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); 
} 
于 2012-04-12T09:21:08.950 回答
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(); 
} 
于 2012-04-12T09:22:08.680 回答
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 创建结果字符串作为性能加速。

于 2012-04-12T09:22:29.980 回答
0

尝试更改此行->

txtResult.Text = line.ToString();  

到:

txtResult.Text += line.ToString();  
于 2013-06-13T08:28:50.217 回答