Created
December 19, 2019 22:19
-
-
Save lucasloss/31d725f4de19985498387de7d3c069c1 to your computer and use it in GitHub Desktop.
System.IO.StreamReader extension methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| namespace ExtensionMethods | |
| { | |
| /// <summary> | |
| /// <see cref="StreamReader"/> extension methods. | |
| /// </summary> | |
| public static class StreamReaderExtensions | |
| { | |
| /// <summary> | |
| /// Gets the number of lines of the stream. | |
| /// </summary> | |
| /// <param name="reader">The <see cref="StreamReader"/>.</param> | |
| /// <returns>The number of lines of the stream.</returns> | |
| public static int CountLines(this StreamReader reader) | |
| { | |
| if (reader == null) | |
| { | |
| throw new ArgumentNullException(nameof(reader)); | |
| } | |
| int count = 0; | |
| long previousPosition = reader.BaseStream.Position; | |
| reader.BaseStream.Position = 0; | |
| reader.DiscardBufferedData(); | |
| while (reader.ReadLine() != null) | |
| { | |
| count++; | |
| } | |
| reader.BaseStream.Position = previousPosition; | |
| return count; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment