From 7573954a176a997b67627af92f8cd6f4e962cf42 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 25 Dec 2018 13:03:30 -0500 Subject: [PATCH] Added StreamReader.SkipLines() extension method. --- .../StreamReader.SkipLines.cs | 23 +++++++++++ src/Z.IO/Z.IO.csproj | 1 + .../StreamReader.SkipLines.cs | 40 +++++++++++++++++++ test/Z.IO.Test/Z.IO.Test.csproj | 1 + 4 files changed, 65 insertions(+) create mode 100644 src/Z.IO/System.IO.StreamReader/StreamReader.SkipLines.cs create mode 100644 test/Z.IO.Test/System.IO.StreamReader/StreamReader.SkipLines.cs diff --git a/src/Z.IO/System.IO.StreamReader/StreamReader.SkipLines.cs b/src/Z.IO/System.IO.StreamReader/StreamReader.SkipLines.cs new file mode 100644 index 00000000..f15d7881 --- /dev/null +++ b/src/Z.IO/System.IO.StreamReader/StreamReader.SkipLines.cs @@ -0,0 +1,23 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System.IO; + +public static partial class Extensions +{ + /// + /// Skips the specified number of lines in a stream reader based on its current position. + /// + /// The stream reader. + /// The number of lines to skip. + public static void SkipLines(this StreamReader @this, int skipCount) + { + for (var i = 0; i < skipCount && !@this.EndOfStream; i++) + { + @this.ReadLine(); + } + } +} \ No newline at end of file diff --git a/src/Z.IO/Z.IO.csproj b/src/Z.IO/Z.IO.csproj index 6b834aa1..7d75ae64 100644 --- a/src/Z.IO/Z.IO.csproj +++ b/src/Z.IO/Z.IO.csproj @@ -110,6 +110,7 @@ + diff --git a/test/Z.IO.Test/System.IO.StreamReader/StreamReader.SkipLines.cs b/test/Z.IO.Test/System.IO.StreamReader/StreamReader.SkipLines.cs new file mode 100644 index 00000000..6b531d5d --- /dev/null +++ b/test/Z.IO.Test/System.IO.StreamReader/StreamReader.SkipLines.cs @@ -0,0 +1,40 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System; +using System.IO; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.IO.Test +{ + [TestClass] + public class System_IO_StreamReader_SkipLines + { + [TestMethod] + public void SkipLines() + { + var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, + "Examples_System_IO_FileInfo_SkipLines.txt"); + + File.WriteAllLines(filePath, new [] + { + "Line1", + "Line2", + "Line3", + "Line4" + }); + + using (var file = File.OpenText(filePath)) + { + file.SkipLines(2); + var nextLine = file.ReadLine(); + + // Unit Test + Assert.AreEqual("Line3", nextLine); + } + } + } +} \ No newline at end of file diff --git a/test/Z.IO.Test/Z.IO.Test.csproj b/test/Z.IO.Test/Z.IO.Test.csproj index 0081a781..32275632 100644 --- a/test/Z.IO.Test/Z.IO.Test.csproj +++ b/test/Z.IO.Test/Z.IO.Test.csproj @@ -100,6 +100,7 @@ +