This documentation is archived and is not being maintained.

File.AppendAllLines Method (String, IEnumerable<String>)

Appends lines to a file, and then closes the file.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static void AppendAllLines(
	string path,
	IEnumerable<string> contents


Type: System.String
The file to append the lines to. The file is created if it does not already exist.
Type: System.Collections.Generic.IEnumerable<String>
The lines to append to the file.


path is a zero-length string, contains only white space, or contains one more invalid characters defined by the GetInvalidPathChars method.


Either path or contents is null.


path is invalid (for example, it is on an unmapped drive).


The file specified by path was not found.


An I/O error occurred while opening the file.


path exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.


path is in an invalid format.


The caller does not have permission to write to the file.


path specifies a file that is read-only.


This operation is not supported on the current platform.


path is a directory.


The caller does not have the required permission.

If the target file does not exist, it is created.

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

using System;
using System.IO;
using System.Linq;

class Program
	static string dataPath = @"c:\temp\timestamps.txt";

	static void Main(string[] args)

		var JulyWeekends = from line in File.ReadLines(dataPath)
						   where (line.StartsWith("Saturday") || 
						   line.StartsWith("Sunday")) & 
						   select line;

		File.WriteAllLines(@"C:\temp\selectedDays.txt", JulyWeekends);

		var MarchMondays = from line in File.ReadLines(dataPath)
						   where line.StartsWith("Monday") && 
						   select line;

		File.AppendAllLines(@"C:\temp\selectedDays.txt", MarchMondays);

	static void CreateSampleFile()
		DateTime TimeStamp = new DateTime(1700, 1, 1);

		using (StreamWriter sw = new StreamWriter(dataPath))
			for (int i = 0; i < 500; i++)
				DateTime TS1 = TimeStamp.AddYears(i);
				DateTime TS2 = TS1.AddMonths(i);
				DateTime TS3 = TS2.AddDays(i);

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.