C # search text file, return all lines containing word

I need help with the program that I create on my internship. The idea is to check how often the user logs on to any PC. When a user logs in, this information is recorded in a text file, for example, in this format.

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

Now I need to search for a text file and (for example) search for a text file for all login dates for the user Chsbe.

My code is:

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(); 
        } 

My question is: how do I return all log lines containing searchterm to txtResult?

+5
source share
7 answers

. , , .
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 MultiLine true, .
, using - , ,

+6

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

yourList = ();

txtResult.Text = line.ToString();
yourList.Add();

"yourList" ,

0

Something like the following might help you get started with 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); 
} 
0
source
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
source

Perhaps something like this will work.

    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(); 
    } 

This will be my version:

    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");
        }
    }

If the list is really long, I would use StringBuilder to create the result string as a performance boost.

0
source

Try changing this line →

txtResult.Text = line.ToString();  

at

txtResult.Text += line.ToString();  
0
source

All Articles