File::WriteAllLines Method (String^, IEnumerable<String^>^)


Creates a new file, writes a collection of strings to the file, and then closes the file.

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

static void WriteAllLines(
	String^ path,
	IEnumerable<String^>^ contents


Type: System::String^

The file to write to.

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

The lines to write to the file.

Exception Condition

path is a zero-length string, contains only white space, or contains one or 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).


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 default behavior of the WriteAllLines(String^, IEnumerable<String^>^) method is to write out data by using UTF-8 encoding without a byte order mark (BOM). If it is necessary to include a UTF-8 identifier, such as a byte order mark, at the beginning of a file, use the WriteAllLines(String^, IEnumerable<String^>^, Encoding^) method overload with UTF8 encoding.

If the target file already exists, it is overwritten.

You can use this method to create the contents for a collection class that takes an IEnumerable<T> in its constructor, such as a List<T>, HashSet<T>, or a SortedSet<T> class.

The following example writes selected lines from a sample data file to a file.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 10
.NET Framework
Available since 4.0
Available since 4.0
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft