Search Amazon:

Read text files in .NET using the System.IO StreamReader

The easiest way is to use the StreamReader class in the System.IO namespace.

Create a form, drop on the OpenFileDialog control and set its Filter property to Text (*.txt) |*.txt. Add a button and in its Click event add the following C# code:

    openFileDialog.ShowDialog();
    string myFile = openFileDialog.FileName;

Open the file:

    Using System.IO;

    string strLine = "";
    StreamReader rdr;

    try
    {
        rdr = new StreamReader(myFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error Opening File", MessageBoxButtons.OK);
        return;
    }

Loop through the file:

    while (strLine != null)
    {
        strLine = rdr.ReadLine();
        if ((strLine != null) && (strLine != ""))
        {
         ... process the line of text ...;
        }
    }
    rdr.Close();
Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms