File.WriteAllLines Method (String, IEnumerable(Of String))
Creates a new file, writes a collection of strings to the file, and then closes the file.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- path
-
Type:
System.String
The file to write to.
- contents
-
Type:
System.Collections.Generic.IEnumerable(Of String)
The lines to write to the file.
| 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 null. |
| DirectoryNotFoundException | path is invalid (for example, it is on an unmapped drive). |
| 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 default behavior of the WriteAllLines(String, IEnumerable(Of 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(Of 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(Of T) in its constructor, such as a List(Of T), HashSet(Of T), or a SortedSet(Of T) class.
The following example writes selected lines from a sample data file to a file.
Imports System Imports System.IO Imports System.Linq Class Program Shared dataPath As String = "c:\temp\timestamps.txt" Public Shared Sub Main(ByVal args As String()) CreateSampleFile() Dim JulyWeekends = From line In File.ReadLines(dataPath) _ Where (line.StartsWith("Saturday") OrElse _ line.StartsWith("Sunday")) And line.Contains("July") _ Select line File.WriteAllLines("C:\temp\selectedDays.txt", JulyWeekends) Dim MarchMondays = From line In File.ReadLines(dataPath) _ Where line.StartsWith("Monday") AndAlso line.Contains("March") _ Select line File.AppendAllLines("C:\temp\selectedDays.txt", MarchMondays) End Sub Private Shared Sub CreateSampleFile() Dim TimeStamp As New DateTime(1700, 1, 1) Using sw As New StreamWriter(dataPath) For i As Integer = 0 To 499 Dim TS1 As DateTime = TimeStamp.AddYears(i) Dim TS2 As DateTime = TS1.AddMonths(i) Dim TS3 As DateTime = TS2.AddDays(i) sw.WriteLine(TS3.ToLongDateString()) Next End Using End Sub End Class
Available since 10
.NET Framework
Available since 4.0
Silverlight
Available since 4.0