How to use C# Textreader Class

In C#, TextReader and TextWriter provide an alternative approach for reading and writing files, distinct from stream classes, despite not being classified as stream classes themselves. Notably, the StreamReader and StreamWriter classes are derived from TextReader and TextWriter, respectively, further extending their capabilities.

Textreader Class

TextReader and TextWriter offers a valuable means of manipulating files at a character level, providing precise control over reading and writing textual content. These classes present an alternative pathway to file operations in C#, enhancing flexibility and expanding the range of available techniques for file manipulation within the language.

Full Source C#
using System; using System.Windows.Forms; using System.IO; using System.Text; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { System.IO.TextWriter writeFile = new StreamWriter("c:\\textwriter.txt"); writeFile.WriteLine("csharp.net-informations.com"); writeFile.Flush(); writeFile.Close(); writeFile = null; } catch (IOException ex) { MessageBox.Show(ex.ToString()); } } } }

When you execute this C# source code , you will get a file TextWriter.txt and inside its written csharp.net-informations.com .

Conclusion

Using the TextReader, one can effectively read a sequential series of characters, while TextWriter facilitates the writing of characters to a file. The StreamReader and StreamWriter classes, in turn, specialize in reading characters from streams and strings, and writing characters to streams and strings, respectively.