SIGN UP MEMBER LOGIN:    
ARTICLE

How to read a text file in C#

Posted by Mahesh Chand Articles | Files, Directories in C# July 30, 2010
This code sample shows how to read text files in C#.
Reader Level:
Download Files:
 

We can use StreamReader to read text files. The following code snippet reads a file and writes on the console line by line.

string fileName = @"C:\Mahesh\McTextFile.txt";

 

// Read a text file using StreamReader

using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))

{

    String line;

    while ((line = sr.ReadLine()) != null)

    {

        Console.WriteLine(line);

    }

}

 

Console.WriteLine("StreamReader Done");

Console.WriteLine("========================");




Another approach is to use File.OpenText method and read a text file.

 

// Read using File.OpenText

if (System.IO.File.Exists(fileName))

{

    using (System.IO.StreamReader sr = System.IO.File.OpenText(fileName))

    {

        String input;

        while ((input = sr.ReadLine()) != null)

        {

            Console.WriteLine(input);

        }

        Console.WriteLine("Finished.");

    }

}

else

{

    Console.WriteLine("File not found");

}

           

Console.WriteLine("File.Opentext Done - Neel wrote it");

 

Console.ReadKey();


Login to add your contents and source code to this article
share this article :
post comment
 

What data? Are you talking about each word? If yes, you will have to read text file in an array with split with space and then sort the array.

Posted by Mahesh Chand May 10, 2011

This is a very helpful article. Please can you explain supposing the file is unsorted and after reading the text file you now want to sort the data it contains, display it to a rich text box and write it to a new file?

Posted by Caleb Adeyemo May 04, 2011

I am big fan of your codes. Its very much clear and understandable.Thanks a lot. I have a question to ask you.How to read a text file work by word.I want to update a string in my text file.Can you please help me.

Posted by Karthiga M Mar 18, 2011

No.
You need to use HttpWebClient to read from the web. Search this site for sample code.

Posted by Mahesh Chand Dec 01, 2010

would this work?  string fileName = @"www.domain.com/folder/file.txt";  ?

Posted by dumb geek Dec 01, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor