Read Data from text file in .Net ( VB and C#)



This tutorial will go over how to read data from a text file in VB.Net and C#.Net

.Net uses StreamReader Class To read data. StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.

VB .Net

Dim line As String = ""
Dim objReader As New System.IO.StreamReader("d:\abc.txt", True) 
Do line = objReader.ReadLine()
Console.WriteLine(line)
Loop Until line Is Nothing
objReader.Close()

C# .Net
using (StreamReader objReader = new StreamReader("abc.txt")) 
{
    string line;
    // Read and display lines from the file
    while ((line = objReader.ReadLine()) != null) 
    {
         Console.WriteLine(line);
    }
}

0 comments :