How do I create a text file?

Category: System.IO, viewed: 3K 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();
            }
        }
    }
}
Powered by Disqus