Append the specified string to the end of a file in CSharp

Description

The following code shows how to append the specified string to the end of a file.

Example


/*from  w  w w  .  j  ava2 s  . c  o  m*/
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"Main.cs";
        if (!File.Exists(path))
        {
            string createText = "Hello " + Environment.NewLine;
            File.WriteAllText(path, createText);
        }
        string appendText = "extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip