Freigeben über


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When it is called by trusted applications, 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)

Syntax

'Declaration
Public Shared Sub WriteAllLines ( _
    path As String, _
    contents As IEnumerable(Of String) _
)
public static void WriteAllLines(
    string path,
    IEnumerable<string> contents
)

Parameters

Exceptions

Exception Condition
ArgumentException

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

ArgumentNullException

Either path or contents is nulla null reference (Nothing in Visual Basic).

DirectoryNotFoundException

path is invalid (for example, 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.

Remarks

For more information about trusted applications, see Trusted Applications.

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>, or HashSet<T> class.

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.