Text File Create

In this chapter you will learn:

  1. Create text file using File.CreateText

Create text file using File.CreateText

using System;//  ja va2s.  co m
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();
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to append to text file
  2. Append file with StreamWriter
Home » C# Tutorial » Stream
Stream classes
Text File Read
Text File write
Text File Create
Text File Append
Replace File Content
BinaryReader
BinaryWriter
FileStream Create
FileStream byte read and write
BufferedStream
Compare File
File Copy
File Copy with FileStream
MemoryStream
Object Serialization
String Writer