»
S
I
D
E
B
A
R
«
FileStream StreamReader in C#
November 22nd, 2009 by admin

One way to go in a file and read the lines in C# is to use FileStream and StreamReader together, you should always use while loop instead of for loop, otherwise, you will get the error I keep getting for this is: Cannot convert type ‘char’ to ’string’.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace testing
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string file = @"C:\Temp\New Folder\New Text Document.txt";
            using(FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                using(StreamReader sr = new StreamReader(fs))
                {
                    while(!sr.EndOfStream)
                    {
                       Console.WriteLine(sr.ReadLine());
                    }
                }
            }
        }
    }
}
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay



Similar Posts:
Leave a Reply

Powered by WP Hashcash

»  Substance: WordPress   »  Style: Ahren Ahimsa