Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to use C Sharp TextWriter Class



C# Textreader and TextWriter are another way to read and write a file, respectively, even though these do not stream classes. The StreamReader and StreamWriter classes are derived from TextReader and TextWriter classes, respectively. The following C# sample source code is showing how to create and write in a file using C# TextWriter.

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("getsuyodev.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 getsuyodev.com.


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More