Export (0) Print
Expand All

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

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.

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

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


Type: System.String

The file to append the lines to. The file is created if it doesn't already exist.

Type: System.Collections.Generic.IEnumerable<String>

The lines to append to the file.

Type: System.Text.Encoding

The character encoding to use.


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


Either path, contents, or encoding is null.


path is invalid (for example, the directory doesn’t exist or 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 the required permission.


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.

The method creates the file if it doesn’t exist, but it doesn't create new directories. Therefore, the value of the path parameter must contain existing directories.

You can use this method to create a file that contains the following:

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft