2012-04-12 44 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(); 
相關問題