แก้ไข

แชร์ผ่าน


File.AppendAllLines Method

Definition

Appends lines to a file, and then closes the file.

Overloads

AppendAllLines(String, IEnumerable<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.

AppendAllLines(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.

AppendAllLines(String, IEnumerable<String>)

Source:
File.cs
Source:
File.cs
Source:
File.cs

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.

public:
 static void AppendAllLines(System::String ^ path, System::Collections::Generic::IEnumerable<System::String ^> ^ contents);
public static void AppendAllLines (string path, System.Collections.Generic.IEnumerable<string> contents);
static member AppendAllLines : string * seq<string> -> unit
Public Shared Sub AppendAllLines (path As String, contents As IEnumerable(Of String))

Parameters

path
String

The file to append the lines to. The file is created if it doesn't already exist.

contents
IEnumerable<String>

The lines to append to the file.

Exceptions

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 null.

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.

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.

-or-

This operation is not supported on the current platform.

-or-

path is a directory.

Examples

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.

using System;
using System.IO;
using System.Linq;

class Program
{
    static string dataPath = @"c:\temp\timestamps.txt";

    static void Main(string[] args)
    {
        CreateSampleFile();

        var JulyWeekends = from line in File.ReadLines(dataPath)
                           where (line.StartsWith("Saturday") ||
                           line.StartsWith("Sunday")) &
                           line.Contains("July")
                           select line;

        File.WriteAllLines(@"C:\temp\selectedDays.txt", JulyWeekends);

        var MarchMondays = from line in File.ReadLines(dataPath)
                           where line.StartsWith("Monday") &&
                           line.Contains("March")
                           select line;

        File.AppendAllLines(@"C:\temp\selectedDays.txt", MarchMondays);
    }

    static void CreateSampleFile()
    {
        DateTime TimeStamp = new DateTime(1700, 1, 1);

        using (StreamWriter sw = new StreamWriter(dataPath))
        {
            for (int i = 0; i < 500; i++)
            {
                DateTime TS1 = TimeStamp.AddYears(i);
                DateTime TS2 = TS1.AddMonths(i);
                DateTime TS3 = TS2.AddDays(i);
                sw.WriteLine(TS3.ToLongDateString());
            }
        }
    }
}
open System
open System.IO

let dataPath = @"c:\temp\timestamps.txt"

let createSampleFile () =
    let timeStamp = DateTime(1700, 1, 1)

    use sw = new StreamWriter(dataPath)

    for i = 0 to 499 do
        let ts1 = timeStamp.AddYears i
        let ts2 = ts1.AddMonths i
        let ts3 = ts2.AddDays i
        ts3.ToLongDateString() |> sw.WriteLine

createSampleFile ()

let julyWeekends =
    File.ReadLines dataPath
    |> Seq.filter (fun line ->
        (line.StartsWith "Saturday"
         || line.StartsWith "Sunday")
        && line.Contains "July")

File.WriteAllLines(@"C:\temp\selectedDays.txt", julyWeekends)

let marchMondays =
    File.ReadLines dataPath
    |> Seq.filter (fun line -> line.StartsWith "Monday" && line.Contains "March")

File.AppendAllLines(@"C:\temp\selectedDays.txt", marchMondays)
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

Remarks

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.

Applies to

AppendAllLines(String, IEnumerable<String>, Encoding)

Source:
File.cs
Source:
File.cs
Source:
File.cs

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.

public:
 static void AppendAllLines(System::String ^ path, System::Collections::Generic::IEnumerable<System::String ^> ^ contents, System::Text::Encoding ^ encoding);
public static void AppendAllLines (string path, System.Collections.Generic.IEnumerable<string> contents, System.Text.Encoding encoding);
static member AppendAllLines : string * seq<string> * System.Text.Encoding -> unit
Public Shared Sub AppendAllLines (path As String, contents As IEnumerable(Of String), encoding As Encoding)

Parameters

path
String

The file to append the lines to. The file is created if it doesn't already exist.

contents
IEnumerable<String>

The lines to append to the file.

encoding
Encoding

The character encoding to use.

Exceptions

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

Either path, contents, or encoding is null.

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.

path is in an invalid format.

The caller does not have the required permission.

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

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:

Applies to