Create text file using File.CreateText : File Create « File Directory Stream « C# / CSharp Tutorial






using System;
using System.IO;

class MainClass
{
  static void Main(string[] args)
  {
    StreamWriter MyStream = null;
    string MyString = "Hello World";

    try
    {
      MyStream = File.CreateText("MyFile.txt");
      MyStream.Write(MyString);
    }
    catch (IOException e)
    {
      Console.WriteLine(e);
    }
    catch (Exception e)
    {
      Console.WriteLine(e);
    }
    finally
    {
      if (MyStream != null)
        MyStream.Close();
    }
  }
}








15.2.File Create
15.2.1.Creating Files
15.2.2.Create a file and get its creation time, full name and Attributes
15.2.3.Create text file using File.CreateText