How to skip first line and start reading file from second line in C#

前端 未结 3 1311
闹比i
闹比i 2021-01-14 15:30

How to start reading file from 2nd line skipping 1st line. This seems to work but is it best way to do so?

            using (StreamReader sr = new StreamRea         


        
3条回答
  •  天命终不由人
    2021-01-14 16:04

    I'm sorry but I see no problem with the way you are doing it though. I couldn't add comment.

    So just for the sake of answering, you probably could have try to call ReadLine() once before the loop. Might not be the best way as I don't know whats the behavior of running ReadLine() if its already end of stream, but it nothing is gonna happen then thats gonna save you some checks.

    Updated:

    To give a more complete answer, calling ReadLine() when the stream is at its end will return a null.

    Reference: http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx

    Remember to check the return for null value.

提交回复
热议问题