Skip to content

Commit 860fed7

Browse files
Add Count && SkipLines
Add Count && SkipLines
1 parent b94c79e commit 860fed7

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 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
2+
// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods
3+
// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues
4+
// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
//
8+
9+
using System;
10+
using System.IO;
11+
using System.Linq;
12+
13+
public static partial class Extensions
14+
{
15+
/// <summary>Gets the total number of lines in a file. </summary>
16+
/// <param name="this">The file to perform the count on.</param>
17+
/// <returns>The total number of lines in a file. </returns>
18+
public static int CountLines(this FileInfo @this)
19+
{
20+
return File.ReadAllLines(@this.FullName).Length;
21+
}
22+
23+
/// <summary>Gets the total number of lines in a file that satisfy the condition in the predicate function.</summary>
24+
/// <param name="this">The file to perform the count on.</param>
25+
/// <param name="predicate">A function to test each line for a condition.</param>
26+
/// <returns>The total number of lines in a file that satisfy the condition in the predicate function.</returns>
27+
public static int CountLines(this FileInfo @this, Func<string, bool> predicate)
28+
{
29+
return File.ReadAllLines(@this.FullName).Count(predicate);
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 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
2+
// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods
3+
// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues
4+
// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
using System.IO;
9+
using System.Linq;
10+
11+
public static partial class Extensions
12+
{
13+
/// <summary>
14+
/// Skips the specified number of lines in a stream reader based on its current position.
15+
/// </summary>
16+
/// <param name="this">The stream reader.</param>
17+
/// <param name="skipCount">The number of lines to skip.</param>
18+
public static void SkipLines(this StreamReader @this, int skipCount)
19+
{
20+
for (var i = 0; i < skipCount && !@this.EndOfStream; i++)
21+
{
22+
@this.ReadLine();
23+
}
24+
}
25+
}

src/Z.IO/Z.IO.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="System.IO.FileInfo\FileInfo.AppendAllLines.cs" />
9292
<Compile Include="System.IO.FileInfo\FileInfo.AppendAllText.cs" />
9393
<Compile Include="System.IO.FileInfo\FileInfo.ChangeExtension.cs" />
94+
<Compile Include="System.IO.FileInfo\FileInfo.CountLines.cs" />
9495
<Compile Include="System.IO.FileInfo\FileInfo.CreateDirectory.cs" />
9596
<Compile Include="System.IO.FileInfo\FileInfo.EnsureDirectoryExists.cs" />
9697
<Compile Include="System.IO.FileInfo\FileInfo.GetDirectoryFullName.cs" />
@@ -110,6 +111,7 @@
110111
<Compile Include="System.IO.FileInfo\FileInfo.WriteAllBytes.cs" />
111112
<Compile Include="System.IO.FileInfo\FileInfo.WriteAllLines.cs" />
112113
<Compile Include="System.IO.FileInfo\FileInfo.WriteAllText.cs" />
114+
<Compile Include="System.IO.StreamReader\StreamReader.SkipLines.cs" />
113115
<Compile Include="System.IO.Stream\Stream.ReadToEnd.cs" />
114116
<Compile Include="System.IO.Stream\Stream.ToByteArray.cs" />
115117
<Compile Include="System.IO.Stream\Stream.ToMD5Hash.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 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
2+
// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods
3+
// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues
4+
// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
using System;
8+
using System.IO;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
11+
namespace Z.IO.Test
12+
{
13+
[TestClass]
14+
public class System_IO_StreamReader_SkipLines
15+
{
16+
[TestMethod]
17+
public void SkipLines()
18+
{
19+
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_IO_FileInfo_SkipLines.txt");
20+
21+
File.WriteAllLines(filePath, new[]
22+
{
23+
"Line1",
24+
"Line2",
25+
"Line3",
26+
"Line4"
27+
});
28+
29+
using (var file = File.OpenText(filePath))
30+
{
31+
file.SkipLines(2);
32+
var nextLine = file.ReadLine();
33+
34+
// Unit Test
35+
Assert.AreEqual("Line3", nextLine);
36+
}
37+
}
38+
}
39+
}

test/Z.IO.Test/Z.IO.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="System.IO.FileInfo\FileInfo.WriteAllBytes.cs" />
101101
<Compile Include="System.IO.FileInfo\FileInfo.WriteAllLines.cs" />
102102
<Compile Include="System.IO.FileInfo\FileInfo.WriteAllText.cs" />
103+
<Compile Include="System.IO.StreamReader\StreamReader.SkipLines.cs" />
103104
<Compile Include="System.IO.Stream\Stream.ReadToEnd.cs" />
104105
<Compile Include="System.IO.Stream\Stream.ToByteArray.cs" />
105106
<Compile Include="System.IO.Stream\Stream.ToMD5Hash.cs" />

0 commit comments

Comments
 (0)