2011-04-25 81 views
0

我正在嘗試創建以下內容。如何閱讀文本的特定行並將其顯示在消息框中

我的應用程序是Windows應用程序和形式包含以下

  1. 兩個文本框一個名字,另一個用於評論
  2. 日期時間選擇器
  3. 設置和重置按鈕。

現在我的應用程序需要像下面一樣工作。

當我打開我的應用程序時,它需要向我顯示提醒abt評論和名稱誰發表評論指定日期的消息。

爲此我不使用任何DataBase我創建的文件來存儲註釋和名稱和日期。

所以我的問題是我應該如何閱讀文本區分名稱,評論和日期。

我的代碼如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Windows.Forms; 
namespace DateUpdater.Classes 
{ 
    public class FileHandling 
    { 

     public bool WritetoFile(Classes.FileParameters Params) 
     { 


      string directoryPath = Environment.CurrentDirectory.ToString(); 

       directoryPath = directoryPath.ToLower().Replace("\\bin\\debug", "\\Logs\\reminder.txt"); 
       File.AppendAllText(directoryPath, Params.Comments.ToString()); 

       File.AppendAllText(directoryPath, EndCharacter()); 
       File.AppendAllText(directoryPath, Params.Name.ToString()); 
       File.AppendAllText(directoryPath, EndCharacter()); 
       File.AppendAllText(directoryPath, Params.DateToSet.ToString()); 
       File.AppendAllText(directoryPath, EndCharacter()); 

       return true; 

     } 

     public Classes.FileParameters ReadFromFile() 
     { 
      Classes.FileParameters Params; 
      string directoryPath = Environment.CurrentDirectory.ToString(); 

      directoryPath = directoryPath.ToLower().Replace("\\bin\\debug", "\\Logs\\reminder.txt"); 
      // string tempData= File.ReadAllText(directoryPath); 
      var searchTarget = "/n"; 
      foreach (var line in File.ReadLines(directoryPath)) 
      { 
       if (line.Contains(searchTarget)) 
       { 

        break; // then stop 
       } 
      } 

      return null; 

     } 

     public string EndCharacter() 
     { 
      return "/n"; 
     } 

    } 
} 

請給我的解決方案...

+0

可以顯示在reminder.txt – Dinash 2011-04-25 07:46:25

回答

0

使用AppendAllText是次優的,因爲msdn說,打開/創建和每次調用時關閉文件。在你的例子中,你會打開和關閉一個文件六次。 File.AppendText將是一個更好的選擇。如果你把你的自定義行分隔符,並使用系統默認的,你可以這樣做:

public bool WritetoFile(Classes.FileParameters Params) 
{ 
    string directoryPath = Environment.CurrentDirectory.ToString(); 

    directoryPath = directoryPath.ToLower().Replace("\\bin\\debug", "\\Logs\\reminder.txt"); 
    using(StreamWriter stream = File.AppendText(directoryPath)) 
    { 
     stream.Write(Params.Comments.ToString()); 
     stream.Write(SepCharacter); 
     stream.Write(directoryPath, Params.Name.ToString()); 
     stream.Write(SepCharacter); 
     stream.WriteLine(directoryPath, Params.DateToSet.ToString()); 
    } 
    return true; 
} 

public char SepCharacter { get{ return '\t'; } } 

這將會把所有的內容在文本文件中一行,由製表符分隔。行分隔符是默認的\r\n。確保你使用的分隔符不會出現在你的文本中,否則你需要在你的WritetoFile/ReadFromFile例程中屏蔽/取消屏蔽它們。 然後你可以做

public Classes.FileParameters ReadFromFile() 
{ 
    Classes.FileParameters Params; 
    string directoryPath = Environment.CurrentDirectory.ToString(); 

    directoryPath = directoryPath.ToLower().Replace("\\bin\\debug", "\\Logs\\reminder.txt"); 
    var searchTarget = "searchString"; 
    foreach (var line in File.ReadLines(directoryPath)) 
    { 
     if (line.Contains(searchTarget)) 
     { 
      // this will give you a string array of Comment, Name and Date 
      // for every line that contains "searchString" 
      string[] data = line.Split(new char[] { SepCharacter }); 

      break; // then stop 
     } 
    } 

    return null; 
} 

如果您的評論可以包含換行符或製表符,這需要一些關於屏蔽/揭露那些更多的工作。但是這個問題也存在於你的初始例子中。

順便說一句,對不起任何錯別字,我在記事本;-)

+0

耶多數民衆贊成確定thnks樣品這樣做:) – 2011-04-25 11:20:26

相關問題