C# File AppendAllLines(String, IEnumerable, Encoding)

Description

Appends lines to a file by using a specified encoding, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.

Syntax


public static void AppendAllLines(
  string path,//ww w  .  j a  va2 s . c  om
  IEnumerable<string> contents,
  Encoding encoding
)

Parameters

  • path - The file to append the lines to. The file is created if it doesn't already exist.
  • contents - The lines to append to the file.
  • encoding - The character encoding to use.

Example


using System;//from  w  w w .ja  v  a  2  s .  c  o  m
using System.IO;
using System.Linq;
using System.Text;
class Program
{
  static string dataPath = @"c:\temp\timestamps.txt";

  static void Main(string[] args)
  {
    var da = from line in File.ReadLines(dataPath)
               where (line.StartsWith("a"))
               select line;

    File.WriteAllLines(@"C:\temp\selectedDays.txt", da);
    File.AppendAllLines(@"C:\temp\selectedDays.txt", da,Encoding.UTF8);
  }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter