can anyone tell me how to read from a text file, line by line....

thanks....this helped, par i have another problem, i need to search for a string in a line and replace it, do u have a code for this in c#.

You need to put all your content in a string and then replace what you need a and rewrite the text file with a StreamWriter.

Sample code:

StreamReader sr = null;
            FileStream file = null;
            string fileData = String.Empty;

            try
            {
                file = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
                sr = new StreamReader(file, Encoding.UTF8);
                string outputData = String.Empty;
                while (true)
                {
                    string temp = String.Empty;
                    temp = sr.ReadLine();
                    if (temp != null)
                    {
                        temp = temp.Replace("the", "THE");
                        outputData += temp + "\n";
                    }
                    else
                        break;
                }
                Console.WriteLine("OUTPUT:\n{0}", outputData);
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
            finally
            {
                if (sr != null)
                    sr.Close();
                if (file != null)
                    file.Close();
            }
            Console.ReadLine();

You can also use the method ReadToEnd to read everything to one string and then use String.Replace method.
Depending on what you want to do you can also consider using regular expressions.

This article has been dead for over six months. Start a new discussion instead.