ReadLine from FileStream

As I found out the FileStream has no function ReadLine, but just Read. Is there any way to read from filestream line by line? (Has anybody written a function for that maybe)
ssdjgru1Asked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Using encoding:

using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8))

Bob
0
 
AlexFMCommented:
You can use StreamReader class:

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt"))
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

0
 
AlexFMCommented:
Having FileStream instance you can create StreamReader from it using constructor:
public StreamReader(Stream stream);


            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {

                    while (sr.Peek() >= 0)
                    {
                        Console.WriteLine(sr.ReadLine());
                    }
                }
            }
0
 
ssdjgru1Author Commented:
the sr.ReadLine returns string, but what i need is byte[]...the conversion can be made easely with getbytes, but the problem is when you have some national specific characters which are instantly converted to unicode in string and render it unuseful for me.
0
 
TheAvengerConnect With a Mentor Commented:
Then convertion to byte[]:

System.Text.UTF8Encoding.GetBytes (sr.ReadLine());
0
All Courses

From novice to tech pro — start learning today.