문제

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