File.AppendAllLines 方法

定义

向一个文件中追加行,然后关闭该文件。

重载

AppendAllLines(String, IEnumerable<String>)

向一个文件中追加行,然后关闭该文件。 如果指定文件不存在,此方法会创建一个文件,向其中写入指定的行,然后关闭该文件。

AppendAllLines(String, IEnumerable<String>, Encoding)

使用指定的编码向一个文件中追加行,然后关闭该文件。 如果指定文件不存在,此方法会创建一个文件,向其中写入指定的行,然后关闭该文件。

AppendAllLines(String, IEnumerable<String>)

向一个文件中追加行,然后关闭该文件。 如果指定文件不存在,此方法会创建一个文件,向其中写入指定的行,然后关闭该文件。

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

要向其中追加行的文件。 如果文件尚不存在,则创建该文件。

contents
IEnumerable<String>

要追加到文件中的行。

例外

path 为长度为零的字符串,仅包含空格,或包含一个或多个由 GetInvalidPathChars() 方法定义的无效字符。

pathcontentsnull

path 无效(例如,目录不存在或在未映射的驱动器上)。

未找到 path 指定的文件。

打开文件时发生 I/O 错误。

path 超出系统定义的最大长度。

path 的格式无效。

调用方没有写入到文件中的权限。

path 指定只读文件。

  • 或 - 当前平台不支持此操作。

  • 或 - path 是一个目录。

示例

下面的示例将示例数据文件中的选定行写入文件,然后追加更多的行。 temp在驱动器 C 上指定的目录必须存在,才能成功完成此示例。

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

注解

如果文件不存在,则该方法将创建该文件,但不会创建新的目录。 因此,参数的值 path 必须包含现有目录。

适用于

AppendAllLines(String, IEnumerable<String>, Encoding)

使用指定的编码向一个文件中追加行,然后关闭该文件。 如果指定文件不存在,此方法会创建一个文件,向其中写入指定的行,然后关闭该文件。

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

要向其中追加行的文件。 如果文件尚不存在,则创建该文件。

contents
IEnumerable<String>

要追加到文件中的行。

encoding
Encoding

要使用的字符编码。

例外

path 为长度为零的字符串,仅包含空格,或包含一个或多个由 GetInvalidPathChars() 方法定义的无效字符。

pathcontentsencodingnull

path 无效(例如,目录不存在或在未映射的驱动器上)。

未找到 path 指定的文件。

打开文件时发生 I/O 错误。

path 超出系统定义的最大长度。

path 的格式无效。

调用方没有所要求的权限。

path 指定只读文件。

  • 或 - 当前平台不支持此操作。

  • 或 - path 是一个目录。

  • 或 - 调用方没有所要求的权限。

注解

如果文件不存在,则该方法将创建该文件,但不会创建新的目录。 因此,参数的值 path 必须包含现有目录。

您可以使用此方法创建包含以下内容的文件:

适用于