Click here to Skip to main content
15,886,783 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to read a text file using C#

I have created open file dialog and selected the file.


How to read individual elements by its keyword
Posted

Hi,

Solution depends on that what you want to do with text. You can read file line by line and extract specific keyword from line or you can read entire text from file to a variable and them parse it.
If file is large text, better solution will be read line by line, but as i said, it depends on your demands.

Reading text files is simple:
C#
using (var streamReader = new StreamReader(@"C:\file.txt"))
{
    // You can read line by line
    while (!streamReader.EndOfStream)
    {
        var line = streamReader.ReadLine();
        // Do something with readed line
    }
    // OR read entire text and assign to variable
    var allText = streamReader.ReadToEnd();
}


Hope it helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900