2011-04-25 78 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

相关问题