Is there any method of reading from a text file and omitting certain lines from the output into a text box?.

the text file will look like this

Name=Test Name 
Date=19/02/14
Message blurb spanning over several lines

The format will always be the same and the Name & Date will always be the 1st 2 rows and these are the rows that i want to omit and return the rest of the message blurb to a text box.

I know how to use the ReadAllLines function and StreamReader but not sure how to start coding it.

Any pointers or directions to some relevant online documentation?

Thanks in advance

有帮助吗?

解决方案

You can read file line by line and just skip lines with given beginnings:

string[] startsToOmit = new string[] { "Name=", "Date=" };

var result = File.ReadLines(path)
                 .Where(line => !startsToOmit.Any(start => line.StartsWith(start)));

and then you have an IEnumerable<string> as a result, you can use it for example by result.ToList().

其他提示

Just read the stream line by line:

        using (StreamReader sr = new StreamReader(path)) 
        {
            Console.WriteLine(sr.ReadLine());
        }

Ignore the first two lines, and process the 3rd line however you need.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top