Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 搜索文本文件,返回包含单词的所有行_C#_Winforms_Full Text Search - Fatal编程技术网

C# 搜索文本文件,返回包含单词的所有行

C# 搜索文本文件,返回包含单词的所有行,c#,winforms,full-text-search,C#,Winforms,Full Text Search,我在实习期间正在建立一个项目,我需要帮助。 其目的是检查用户登录任何PC的频率。 当用户登录时,该信息记录在文本文件中,如以下格式 01-01-2011 16:47:10-002481C218B0-WS3092-Chsbe (XP-D790PRO1) 现在我需要搜索文本文件,并(例如)搜索文本文件以查找用户Chsbe的所有登录日期 到目前为止,我的代码是: private void btnZoek_Click(object sender, EventArgs e) {

我在实习期间正在建立一个项目,我需要帮助。 其目的是检查用户登录任何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(); 
        } 

我的问题是,如何将日志中包含searchterm的所有字符串返回到txtResult?

使用richtextbox或使用multiline属性 比如说

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(); 
    } 
定义一个列表

List yourList=新列表()

更换线路 txtResult.Text=line.ToString()
通过 添加(行)


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

,下面类似的内容可能有助于您开始使用regex:

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

foreach (Match m in mc) 
{ 
    Console.WriteLine(m.Value); 
} 

你已经做得很好了。唯一的错误是将读取的最后一行写入文本框,覆盖上一行。
您需要使用一个StringBuilder和一个
语句来处理您的一次性流,如下所示:

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,否则您将无法看到输出。

请记住,
使用
是处理此类情况的更好方法,因为它也会自动处理意外的文件异常,注意正确关闭流

也许类似的方法会起作用

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(); 
} 
    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");
        }
    }
private void btnZoek_单击(对象发送者,事件参数e)
{
尝试
{
int计数器=0;
弦线;
List LinesFound=新列表();
//读取文件并逐行显示。
System.IO.StreamReader file=new System.IO.StreamReader(“c:\\log.txt”);
而((line=file.ReadLine())!=null)
{
if(line.Contains(txtZoek.Text))
{
LinesFound.Add(行);
}
}
file.Close();
foreach(LinesFound中的字符串行)
{
Text=txtreult.Text+Line+Environment.NewLine;
}
}
捕获(例外)
{
MessageBox.Show(“btnZoek_点击错误”);
}
}

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

尝试更改此行->

txtResult.Text = line.ToString();  
致:


是否可以将源代码获取为xml?如前所述,查看regex。如果搜索变得更复杂,请查看某种全文搜索组件SBTW。我不认为这对初学者有多大帮助。您至少应该实际调用类名。链接到MSDN或显示初始化。。。这将解释更多,也将有“为什么”使用richtextbox。无论如何,我不明白richtextbox与OP问题有什么关系。根据他的问题,他所需要的只是多行文本框和累积文本,而不是替换。如果您使用textbox(我认为TXTRUST是textbox),您应该添加多行属性。Richtextbox在默认情况下使用此属性,我只想显示如何获得所需结果的简单示例。richtextbox1.Text+=“\n”+line.ToString();这是答案,你能把这个答案做对吗?谢谢你的支持!就是这样!谢谢你的快速帮助,史蒂夫。我试过了,但根本没有列出。。你能澄清一下吗?
txtResult.Text += line.ToString();