How do I create a text file?

Category: System.IO, viewed: 874 time(s).

This example show you how to create a text file and write some data into the created text file. In this example we use the TextWriter and StreamWriter class.

using System;
using System.IO;

namespace Kodecsharp.Example.System.IO
{
    public class WriteTextFile
    {
        public static void Main(string[] args)
        {
            //
            // Create a new text file called employee.txt
            //
            TextWriter writer = new StreamWriter(@"C:\employee.txt");

            try
            {
                //
                // Write some string into file.
                //
                writer.WriteLine("John Riise,30-12-76,10000");
                writer.WriteLine("Ricky James,21-02-80,5000");
                writer.WriteLine("Sidney Brickstore,07-11-80,5000");
            }
            finally
            {
                writer.Flush();
                writer.Close();
            }
        }
    }
}
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Our Friends

Network Sites

Statistics

Locations of visitors to this page