C# TextReader

C# TextReader class is found in System.IO namespace. It represents a reader that can be used to read text or sequential series of characters.

Read All Data

Example

Let's see the simple example of TextReader class that reads data till the end of file.

snippet
using System;
using System.IO;
namespace TextReaderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TextReader tr = File.OpenText("e:\\f.txt"))
            {
                Console.WriteLine(tr.ReadToEnd());
            }
        }
    }
}
Output
Hello C# C# File Handling by rookienerd

Read One Line

Example

Let's see the simple example of TextReader class that reads single line from the file.

snippet
using System;
using System.IO;
namespace TextReaderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TextReader tr = File.OpenText("e:\\f.txt"))
            {
                Console.WriteLine(tr.ReadLine());
            }
        }
    }
}
Output
Hello C#
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +