开发者

How to Read specific lines of text and display it on message box [closed]

开发者 https://www.devze.com 2023-02-28 17:53 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am trying to create following.

My app is windows app and form contains following

  1. two text boxes one for name and another for comments
  2. date time picker
  3. set and reset button.

now my app needs to work like following.

When i open my app it needs 开发者_如何学Goto show me the message reminding abt comments and name who made comment specifying date.

for this i m not using any DataBase i created file to store comments and name and date.

so my problem is how i should read text distinguishing name ,comments and date.

my code is as follows:

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

    }
}

please give me the solution...


Using AppendAllText is suboptimal, since msdn says it opens/creates and closes a file every time you call it. In your example, you'd open and close a file six times. File.AppendText will be a better alternative. If you drop your custom line separators and use the system default, you could do this:

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'; } }

This will put all your content in a single line in your text file, separated by a tab. The line separator is the default \r\n. Make sure you use separator characters that won't appear within your text, otherwise you need to mask/unmask them in your WritetoFile/ReadFromFile routines. And then you could do

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

In case your comments can contain line feeds or tabs, this needs some more work regarding masking/unmasking those. But that problem exists in your intial example too as well.

By the way, sorry for any typos, I did this in Notepad ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消