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.
Assembly: mscorlib (in mscorlib.dll)
public static void AppendAllLines( string path, IEnumerable<string> contents, Encoding encoding )
Parameters
- path
-
Type:
System.String
The file to append the lines to. The file is created if it doesn't already exist.
- contents
-
Type:
System.Collections.Generic.IEnumerable<String>
The lines to append to the file.
- encoding
-
Type:
System.Text.Encoding
The character encoding to use.
| Exception | Condition |
|---|---|
| ArgumentException | path is a zero-length string, contains only white space, or contains one more invalid characters defined by the GetInvalidPathChars method. |
| ArgumentNullException | Either path, contents, or encoding is null. |
| DirectoryNotFoundException | path is invalid (for example, the directory doesn’t exist or it is on an unmapped drive). |
| FileNotFoundException | The file specified by path was not found. |
| IOException | An I/O error occurred while opening the file. |
| PathTooLongException | 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. |
| NotSupportedException | path is in an invalid format. |
| SecurityException | The caller does not have the required permission. |
| UnauthorizedAccessException | path specifies a file that is read-only. -or- This operation is not supported on the current platform. -or- path is a directory. -or- 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:
The results of a LINQ to Objects query on the lines of a file, as obtained by using the ReadLines method.
The contents of a collection that implements an IEnumerable<T> of strings.
Available since 10
.NET Framework
Available since 4.0
Silverlight
Available since 4.0