ReadLine from FileStream

Posted on 2005-03-08
Medium Priority
Last Modified: 2011-08-18
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)
Question by:ssdjgru1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Expert Comment

ID: 13485410
You can use StreamReader class:

using System;
using System.IO;

class Test
    public static void Main()
            // 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)
        catch (Exception e)
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");

LVL 48

Expert Comment

ID: 13485430
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)

Author Comment

ID: 13485466
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.
LVL 96

Accepted Solution

Bob Learned earned 252 total points
ID: 13485887
Using encoding:

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

LVL 20

Assisted Solution

TheAvenger earned 248 total points
ID: 13486499
Then convertion to byte[]:

System.Text.UTF8Encoding.GetBytes (sr.ReadLine());

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question