Append the string to the file with encoding in CSharp

Description

The following code shows how to append the string to the file with encoding.

Example


using System;// ww w  .ja  v  a 2  s .  c o m
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\MyTest.txt";
        if (!File.Exists(path))
        {
            string createText = "Hello " + Environment.NewLine;
            File.WriteAllText(path, createText, Encoding.UTF8);
        }

        string appendText = "extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}




















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