C# File AppendAllLines(String, IEnumerable<String>)

Description

Appends lines to a file, 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,
  IEnumerable<string> contents
)

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.

Example

The following example writes selected lines from a sample data file to a file, and then appends more lines.


//w w  w .  ja va 2s. c om
using System;
using System.IO;
using System.Linq;

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);
  }
}




















Home »
  C# Tutorial »
    System.IO »




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