File.AppendAllLines Method (String, IEnumerable(Of String))
Appends lines to a file, 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)
'Declaration Public Shared Sub AppendAllLines ( _ path As String, _ contents As IEnumerable(Of String) _ )
path is a zero-length string, contains only white space, or contains one more invalid characters defined by the GetInvalidPathChars method.
Either path or contents is Nothing.
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 permission to write to the file.
path specifies a file that is read-only.
This operation is not supported on the current platform.
path is a directory.
The following example writes selected lines from a sample data file to a file, and then appends more lines. The directory named temp on drive C must exist for the example to complete successfully.
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
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.