C# File.ReadLines() .net 4.0
In C#.net you can open large file in chunks to read lines one by one or line by line. Here is an example of Read File Lines in Vb.net and C#.net. In .net Framework 4.0, File instance has new static method called ReadLines. Let's see program in C# and Vb.net to read large size files. You may want to process very large file line by line. This method is very useful as it returns line only when needed and saves the resources.
C# File.ReadLines
File.ReadLines provides IEnumerator before loading the entire collection of file in memory. It's very useful method when file is very big in size to load in memory. You can write memory efficient code which will save limited resources.
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
foreach (string line in File.ReadLines(@"C:\xxx\Large_File_example.txt"))
{
if (line.Contains("code") & line.Contains("Satalaj"))
{
Console.WriteLine(line);
}
}
}
}
VB.net program to read large file line by line.
Imports System
Imports System.IO
Imports System.Linq
Class Program
Private Shared Sub Main(args As String())
For Each line As String In File.ReadLines("C:\xxx\Large_File_example.txt")
If line.Contains("code") And line.Contains("Satalaj") Then
Console.WriteLine(line)
End If
Next
End Sub
End Class
Currently rated 5.0 by 2 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5