Reading Data from a Text File Using .NET
Posted by Rhonda Tipton on October 30, 2006
Data can be read from a text file by way of the System.IO namespace. This namespace allows the reading from and writing to files and data streams. The StreamReader class can be used to access a text file and read it line by line.
Below are the main methods in the StreamReader class.

Text File (used in the sample program)
Sample C# Program
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace ReadTextFile { class ReadText { static void Main(string[] args) { //- Location of the Text file string fileName = @"c:temptxt_test.TXT"; //- Set the line counter int lineNumber = 0; //- Write initial text to the console Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); Console.WriteLine("MY FAVORITE SONGS"); Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); //- Open the text file using (StreamReader sr = new StreamReader(fileName)) { //- Initialize name variables string aName = ""; string sName = ""; string alName = ""; string line; //- Holds the entire line //- Cycle thru the text file 1 line at a time pulling //- substrings into the variables initialized above while ((line = sr.ReadLine()) != null) { lineNumber++; //- Pulling substrings. If there is a problem //- with the start index and/or the length values //- an exception is thrown try { aName = line.Substring(0,21).Trim(); sName = line.Substring(22,25).Trim(); alName = line.Substring(47,17).Trim(); } catch (Exception ex) { Console.Write(ex.ToString()); } //- Write the information to the console Console.WriteLine("Artist: {0}", aName); Console.WriteLine("Title : {0}", sName); Console.WriteLine("Album : {0}", alName); Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } sr.Close(); } } } }
—
Sample VB.NET Program
Imports System.IO Module ReadText_vb Sub Main() ‘- Location of the Text file Dim fileName As String = "c:temptxt_test.TXT" ‘- Set the line counter Dim lineNumber As Integer = 0 ‘- Write initial text to the console Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") Console.WriteLine("MY FAVORITE SONGS") Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") ‘- Open the text file/stream Dim sr As StreamReader = New StreamReader(fileName) ‘- Initialize name variables Dim aName As String = "" Dim sName As String = "" Dim alName As String = "" Dim line As String = "" ‘- Cycle thru the text file 1 line at a time pulling ‘- substrings into the variables initialized above Do line = sr.ReadLine() ‘- If it goes past the last line of the file, ‘- it drops out of the loop If line <> Nothing Then Try aName = line.Substring(0, 21).Trim() sName = line.Substring(22, 25).Trim() alName = line.Substring(47, 17).Trim() Catch ex As Exception Console.Write(ex.ToString()) End Try ‘- Write the information to the console Console.WriteLine("Artist: {0}", aName) Console.WriteLine("Title : {0}", sName) Console.WriteLine("Album : {0}", alName) Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") End If Loop Until line Is Nothing ‘- Close the file/stream sr.Close() End Sub End Module
Output

Among many other classes, the System.IO namespace also contains the StreamWriter class which I will go over in a future post.
Related Links
Text Reader and Text Writer in C#






















November 1, 2006 at 2:47 am
[...] I am using the same text file that I used in my previous post as the source file for the below example. [...]
November 1, 2006 at 3:10 am
[...] I am using the same text file that I used in my previous post as the source file for the below example. [...]