C# File.ReadLines() .net 4.0

C# File.ReadLines() .net 4.0 File.ReadLines()

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

Tags:

ASP net Write Text On Image C#.net and Vb.net programAsp.net delete file file on Server C#.net and VB.net codeRemoving aspnet runtime from IISC# foreach loopOne dimensional array example C#.net VB.netRemove internet explorerHow to select multiple columns in Linq- C#.net VB.net codeRead Text File in (C#.net or VB.net)How are you using session variables in Asp.net with C#.netC# 4.0 Tuple ExampleC#.net get tomorrows date

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.