create a text file and save text to it - CSharp File IO

CSharp examples for File IO:Text File

Description

create a text file and save text to it

Demo Code

using System;/*from  w  w w.  j av a 2 s .c o m*/
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using static System.Console;
using System.IO;
class Program
{
   static void Main(string[] args)
   {
      string textFile = "streams.txt";
      // create a text file and return a helper writer
      StreamWriter text = File.CreateText(textFile);
      text.WriteLine("asdf");
      text.Close(); // close helper
      // output the contents of the file to the Console
      WriteLine($"{textFile} contains {new FileInfo(textFile).Length} bytes.");
      WriteLine(File.ReadAllText(textFile));
   }
}

Result


Related Tutorials