C# SystemIOFileReadAllLines() method
File.ReadAllLines method returns an array of strings containing lines of the file. It's not good idea to load entire file in memory, as resources are limited. Here we will see program that reads all lines in file and returns collection of strings.
C# File ReadAllLines method
using System;
namespace Csharp.ReadFile.Line.By.Line.Example
{
class Program
{
static void Main(string[] args)
{
foreach (string line in System.IO.File.ReadAllLines(@"C:\xxx\ReadMe.txt"))
{
Console.ReadLine(line);
}
Console.ReadLine();
}
}
}
VB.net File RealAllLines example
Here is Vb.net code snippet which uses static ReadAllLines method of File class.
Namespace Csharp.ReadFile.Line.By.Line.Example
Class Program
Private Shared Sub Main(args As String())
For Each line As String In System.IO.File.ReadAllLines("C:\xxx\ReadMe.txt")
Console.ReadLine(line)
Next
Console.ReadLine()
End Sub
End Class
End Namespace
Description: The static method ReadAllLines of File class returns collection of strings in terms of array. We are iterating that array with the help of foreach line.
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5