2012-04-12 39 views
5

Tôi cần trợ giúp về chương trình tôi đang xây dựng ở cơ quan thực tập của mình. Ý tưởng là kiểm tra tần suất người dùng đăng nhập trên bất kỳ máy tính nào. Khi người dùng đăng nhập, thông tin đó được ghi vào tệp văn bản, như định dạng này.Tệp văn bản tìm kiếm C#, trả lại tất cả các dòng có chứa một từ

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

Bây giờ tôi cần phải tìm kiếm tệp văn bản và (ví dụ) tìm kiếm tệp văn bản cho tất cả ngày đăng nhập cho người dùng Chsbe.

Mã của tôi cho đến nay:

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

Câu hỏi của tôi là, Làm thế nào để tôi trả lại tất cả các chuỗi trong các bản ghi chứa searchterm để txtResult?

+1

nhìn vào regex –

+0

Có possbile để có được những nguồn như xml? Nếu không được đề cập trước đó, hãy xem regex. Nếu tìm kiếm trở nên phức tạp hơn, hãy xem một số loại thành phần tìm kiếm toàn văn –

Trả lời

6

Bạn đang làm một công việc tốt. Lỗi duy nhất là trong văn bản của dòng cuối cùng đọc vào hộp văn bản ghi đè lên trước đó.
Bạn cần phải sử dụng một StringBuilder và một tuyên bố using xung quanh Suối dùng một lần của bạn như thế này:

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

tất nhiên, txtResult của bạn nên có multiline sở hữu thiết lập là true, nếu không bạn sẽ không thể nhìn thấy đầu ra.
Hãy ghi nhớ rằng using là cách tốt hơn để xử lý các loại tình huống vì nó tự động xử lý các trường hợp ngoại lệ cũng tập tin bất ngờ chăm sóc để đóng đúng Suối bạn

+0

Có! đây chính là nó! Cảm ơn vì sự giúp đỡ nhanh chóng, Steve. – Ralph

0

Sử dụng richtextbox hoặc sử dụng tài sản multiline ví dụ

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

Ý của bạn là Richtextbox? – basti

+0

Ồ, vâng, hãy bỏ lỡ nhấp chuột – Likurg

+0

Btw. Tôi không nghĩ rằng điều này sẽ giúp ích rất nhiều cho bộ máy. Bạn nên ít nhất là thực sự gọi tên lớp. Liên kết với MSDN hoặc hiển thị initalisation .. ... điều đó sẽ giải thích nhiều hơn và cũng sẽ có "lý do" sử dụng richtextbox. – basti

0

Xác định Danh sách

Danh sách yourList = new List();

Thay thế dòng txtResult.Text = line.ToString();
by yourList.Add (line);

trong List "yourList" bạn có tất cả các dòng có chứa các tài

+0

Tôi đã thử điều này nhưng nó sẽ không tạo danh sách nào cả .. bạn có thể làm rõ điều này không? – Ralph

+0

Mã của bạn như thế nào? – sebastianmehler

0

Cái gì như dưới đây có thể giúp bạn bắt đầu với 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
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

Có lẽ một cái gì đó như thế này sẽ làm việc.

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

Đây sẽ là phiên bản của tôi:

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

Nếu danh sách thực sự là lâu tôi sẽ sử dụng một StringBuilder để tạo ra một chuỗi kết quả là sự tăng tốc hiệu suất.

0

Hãy thử thay đổi dòng này ->

txtResult.Text = line.ToString(); 

tới:

txtResult.Text += line.ToString(); 
Các vấn đề liên quan