Click here to Skip to main content
12,953,379 members (51,627 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 3 Nov 2005

Read a certain line in a text file

, 3 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Read a certain line in your text files by making an array
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->


This Article will show you how to read a line in a text file. We will write a small class that will take the file that you have loaded and number of lines you want to load. I tired to make this simple as possible.

The Code

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ReadLine
    class Program
        static void Main(string[] args)
            //Load our text file
            TextReader tr = new StreamReader("\\test.txt");

            //How many lines should be loaded?
            int NumberOfLines = 15;

            //Make our array for each line
            string[] ListLines = new string[NumberOfLines];

            //Read the number of lines and put them in the array
            for (int i = 1; i < NumberOfLines; i++)
                ListLines[i] = tr.ReadLine();

            //This will write the 5th line into the console
            //This will write the 1st line into the console


            // close the stream

The Code - Explained

I'll now take the time to let you know what is going on in the code.

TextReader tr = new StreamReader("\\test.txt");

This will just load the text file into the program, make sure that you rename it to whatever your file is.

int NumberOfLines = 15;
string[] ListLines = new string[NumberOfLines];

The NumberOfLines will hold the number of lines you want to load into the array. The ListLines makes our array that will hold each line from our text file.
Note: Make sure that you add one to NumberOfLines, if you have a total of 5 lines make NumberOfLines equal 6.

for (int i = 1; i < NumberOfLines; i++)
    ListLines[i] = tr.ReadLine();

This is the main part of the program that will put each line of code into the ListLines array. This will allow us to call each line whenever we want to.


All you have to do now is use ListLines[NumberLineYouWant] and you can do whatever you want with it.

My Small Background

This is my fist article I've made. I started C# about 2 weeks ago. This might not be the best way (For example you need to get to the 50,000th line in a text file, you'll have to make an array that will hold 50,000 lines of text). I started programming in PHP at the age of 12 (I'm 14 now). Ever since that I've always wanted to learn a new language. I've played with C++ then I started coding in C. To me C was much easier that C++. I have also played with ASP and ASP.NET, but since I was good at PHP, I couldn't find a reason for those languages. Now, here I am with C#, which to me is far easier then C or C++.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralTHis code looks promising Pin
offroaderdan15-Oct-09 7:47
memberoffroaderdan15-Oct-09 7:47 
GeneralCould always use an Arraylist Pin
Xybex4-Nov-05 7:32
memberXybex4-Nov-05 7:32 
GeneralRe: Could always use an Arraylist Pin
due85014-Jan-09 17:26
memberdue85014-Jan-09 17:26 
GeneralProblem with your code Pin
leppie3-Nov-05 22:26
memberleppie3-Nov-05 22:26 
GeneralMake a class out of it Pin
Robert Rohde3-Nov-05 19:43
memberRobert Rohde3-Nov-05 19:43 
GeneralRe: Make a class out of it Pin
Jisu744-Nov-05 11:37
memberJisu744-Nov-05 11:37 
GeneralRe: Make a class out of it Pin
vangogh.vincent1-Oct-07 21:32
membervangogh.vincent1-Oct-07 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 4 Nov 2005
Article Copyright 2005 by Jisu74
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid