In c#, TextWriter is a class of System.IO namespace and it is useful to write a text or sequential series of characters in the file. The TextWriter is an abstract class and it writes the characters to streams and strings.
Now, we will see how to use TextWriter class in c# to write a text or sequential series of characters in the file with examples.
Following is the example of writing a text or sequential series of characters in a file using the TextWriter object in c#.
static void Main(string args)
string fpath = @"D:\Test.txt";
// Check file if exists
// Create the file
using (TextWriter wr = File.CreateText(fpath))
wr.WriteLine("\r\nWelcome to Tutlane");
When we execute the above example, it will create a new “Test.txt” file in D drive with the required text as shown below.
This is how we can create, delete and add text to files using File & TextStream objects in c#.