.Net Framework
C# Tutorial
C# Control Statement
C# Arrays
C# Object Class
C# Properties
C# Inheritance
C# Polymorphism
C# Abstraction
C# Strings
C# Exception Handling
C# File IO
C# Collections
C# Generics
C# Delegates
C# Reflection
Anonymous Function
C# Multithreading
C# Synchronization
C# Web Service
C# Misc
C# New Features
C# Programs
C# Interview Questions
ADO.NET Tutorial
ASP.NET Tutorial
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.
C# TextReader Example: Read All Data
Let's see the simple example of TextReader class that reads data till the end of file.
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());
}
}
}
}
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 Microsoft
C# File Handling by Microsoft
C# TextReader Example: Read One Line
Let's see the simple example of TextReader class that reads single line from the file.
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());
}
}
}
}
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#