File.AppendAllLines 方法

定义

向一个文件中追加行,然后关闭该文件。Appends lines to a file, and then closes the file.

重载

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>)

向一个文件中追加行,然后关闭该文件。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))

参数

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.

例外

path 为长度为零的字符串,仅包含空格,或包含一个或多个由 GetInvalidPathChars() 方法定义的无效字符。path is a zero-length string, contains only white space, or contains one more invalid characters defined by the GetInvalidPathChars() method.

pathcontentsnullEither path or contents is null.

path 无效(例如,目录不存在或在未映射的驱动器上)。path is invalid (for example, the directory doesn't exist or it is on an unmapped drive).

未找到 path 指定的文件。The file specified by path was not found.

打开文件时发生 I/O 错误。An I/O error occurred while opening the file.

path 超出系统定义的最大长度。path exceeds the system-defined maximum length.

path 的格式无效。path is in an invalid format.

调用方没有写入到文件中的权限。The caller does not have permission to write to the file.

path 指定只读文件。path specifies a file that is read-only.

- 或 --or- 当前平台不支持此操作。This operation is not supported on the current platform.

- 或 --or- path 是一个目录。path is a directory.

示例

下面的示例将示例数据文件中的选定行写入文件,然后追加更多的行。The following example writes selected lines from a sample data file to a file, and then appends more lines. temp在驱动器 C 上指定的目录必须存在,才能成功完成此示例。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());
            }
        }
    }
}
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

注解

如果文件不存在,则该方法将创建该文件,但不会创建新的目录。The method creates the file if it doesn't exist, but it doesn't create new directories. 因此,参数的值 path 必须包含现有目录。Therefore, the value of the path parameter must contain existing directories.

适用于

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.

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)

参数

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.

例外

path 为长度为零的字符串,仅包含空格,或包含一个或多个由 GetInvalidPathChars() 方法定义的无效字符。path is a zero-length string, contains only white space, or contains one more invalid characters defined by the GetInvalidPathChars() method.

pathcontentsencodingnullEither path, contents, or encoding is null.

path 无效(例如,目录不存在或在未映射的驱动器上)。path is invalid (for example, the directory doesn't exist or it is on an unmapped drive).

未找到 path 指定的文件。The file specified by path was not found.

打开文件时发生 I/O 错误。An I/O error occurred while opening the file.

path 超出系统定义的最大长度。path exceeds the system-defined maximum length.

path 的格式无效。path is in an invalid format.

调用方没有所要求的权限。The caller does not have the required permission.

path 指定只读文件。path specifies a file that is read-only.

- 或 --or- 当前平台不支持此操作。This operation is not supported on the current platform.

- 或 --or- path 是一个目录。path is a directory.

- 或 --or- 调用方没有所要求的权限。The caller does not have the required permission.

注解

如果文件不存在,则该方法将创建该文件,但不会创建新的目录。The method creates the file if it doesn't exist, but it doesn't create new directories. 因此,参数的值 path 必须包含现有目录。Therefore, the value of the path parameter must contain existing directories.

您可以使用此方法创建包含以下内容的文件:You can use this method to create a file that contains the following:

适用于