hi,
to read from a notepad i hv used the following code.
bt the problem is, it will not read the next line.
so how to read line by line.....

using (StreamReader sr = new StreamReader("c: \\key.txt"))
            {
              string s = sr.ReadLine();
              //etc.
               Console.WriteLine(s);//its showing the output
               string[] variables = s.Split(' ');
               float fvalue = float.Parse(variables[0]);
               Console.WriteLine(fvalue);
            }

Edited 7 Years Ago by __avd: Use [code] tags to wrap your code.

You need a "while" loop.
something like:

//Open file here

   while(!sr.EndOfStream)
   {
      string s = sr.ReadLine();
      //...
   }

   /// close file here

With a using clause you don't have to worry about closing a file. The compiler infers that for you and takes the appropriate actions!

Found an old code snippet, Here goes.

static void Main(string[] args)
        {
            string line;

            try
            {
                StreamReader sr = new StreamReader(new FileStream("C:\\Techdata.txt", FileMode.Open));

                line = sr.ReadLine();
                while (line != null)
                {
                    Console.WriteLine(line);
                    line = sr.ReadLine();
                }
                sr.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }

Edited 7 Years Ago by sanch01r: n/a

Well, you need it to only stop reading when the document is over so use a loop of some sort. A while loop is the best.

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