streamreader c# code example

Example 1: c# streamreader

// The StreamReader are using the Namespaces: System and system.IO

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
  string line;
  while ((line = sr.ReadLine()) != null)
  {
    Console.WriteLine(line);
  }
}

Example 2: c# read file stream

using System;
using System.IO;
using System.Text;

namespace StreamReaderReadToEnd
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = @"C:\Users\Jano\Documents\thermopylae.txt";

            using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            using var sr = new StreamReader(fs, Encoding.UTF8);
            
            string content = sr.ReadToEnd();

            Console.WriteLine(content);
        }
    }
}

Example 3: how to read a text file C#

string[] text = System.IO.File.ReadAllLines(@"C:\users\username\filepath\file.txt");

Example 4: C# using StreamReader

using System.IO; // <- This is the class for StreamReader

Example 5: streamreader c#

streamreader c#

Tags:

Misc Example