Read Text File in (C#.net or VB.net)

Read Text File in (C#.net or VB.net) C#-Read-Text-File

Reading text file using C# or VB.net is one of the basic programming construct and I bet you all had done it. Here are the examples which reads text file.

C# read file example

using System;
using System.IO;
namespace Csharp.ReadFile.Example
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\xxx";
if (!File.Exists(filePath))
{
filePath  = filePath + @"\" + "REadme.txt";
FileStream fs = new FileStream(filePath, FileMode.Open);
StreamReader reader = new StreamReader(fs);
Console.WriteLine(reader.ReadToEnd());
Console.ReadLine();
}
}
}
}
Output
This is first line
This is second line
This is third line
Imports System.IO
Namespace Csharp.ReadFile.Example
Class Program
Private Shared Sub Main(args As String())
Dim filePath As String = "C:\xxx"
If Not File.Exists(filePath) Then
filePath = filePath & "\" & "REadme.txt"
Dim fs As New FileStream(filePath, FileMode.Open)
Dim reader As New StreamReader(fs)
Console.WriteLine(reader.ReadToEnd())
Console.ReadLine()
End If
End Sub
End Class
End Namespace

Tags: