File.AppendAllText メソッド

定義

指定した文字列をファイルに追加します。ファイルがまだ存在しない場合は、ファイルを作成します。

オーバーロード

AppendAllText(String, String)

ファイルを開き、指定した文字列をそのファイルに追加した後、ファイルを閉じます。 ファイルが存在しない場合、このメソッドはファイルを作成し、指定した文字列をファイルに書き込んだ後、ファイルを閉じます。

AppendAllText(String, String, Encoding)

指定のエンコードを使用して指定の文字列をファイルに追加し、ファイルが存在しない場合は作成します。

AppendAllText(String, String)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

ファイルを開き、指定した文字列をそのファイルに追加した後、ファイルを閉じます。 ファイルが存在しない場合、このメソッドはファイルを作成し、指定した文字列をファイルに書き込んだ後、ファイルを閉じます。

public:
 static void AppendAllText(System::String ^ path, System::String ^ contents);
public static void AppendAllText (string path, string contents);
public static void AppendAllText (string path, string? contents);
static member AppendAllText : string * string -> unit
Public Shared Sub AppendAllText (path As String, contents As String)

パラメーター

path
String

指定した文字列の追加先となるファイル。

contents
String

ファイルに追加する文字列。

例外

.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: path は長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。

pathnullです。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

指定されたパスが有効ではありません (たとえば、ディレクトリが存在しない、またはマップされていないドライブにあるなど)。

ファイルを開くときに、I/O エラーが発生しました。

path が読み取り専用のファイルを指定しました。

- または -

この操作は、現在のプラットフォームではサポートされていません。

- または -

path がディレクトリを指定しました。

- または -

呼び出し元に、必要なアクセス許可がありません。

path の形式が正しくありません。

呼び出し元に、必要なアクセス許可がありません。

次のコード例では、 メソッドを使用してファイルの AppendAllText 末尾にテキストを追加する方法を示します。 この例では、ファイルがまだ存在しない場合に作成され、テキストが追加されます。 ただし、この例を正常に完了するには、ドライブ C に という名前 temp のディレクトリが存在する必要があります。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
open System
open System.IO

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText =
        "Hello and Welcome" + Environment.NewLine

    File.WriteAllText(path, createText)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText As String = "Hello and Welcome" + Environment.NewLine
            File.WriteAllText(path, createText)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText)

        ' Open the file to read from.
        Dim readText As String = File.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

注釈

文字列とファイル パスを指定すると、このメソッドは指定したファイルを開き、ファイルの末尾に文字列を追加して、ファイルを閉じます。 例外が発生した場合でも、ファイル ハンドルはこのメソッドによって閉じられることが保証されます。

メソッドは、存在しない場合はファイルを作成しますが、新しいディレクトリは作成しません。 したがって、 パラメーターの値には既存のディレクトリが path 含まれている必要があります。

適用対象

AppendAllText(String, String, Encoding)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

指定のエンコードを使用して指定の文字列をファイルに追加し、ファイルが存在しない場合は作成します。

public:
 static void AppendAllText(System::String ^ path, System::String ^ contents, System::Text::Encoding ^ encoding);
public static void AppendAllText (string path, string contents, System.Text.Encoding encoding);
public static void AppendAllText (string path, string? contents, System.Text.Encoding encoding);
static member AppendAllText : string * string * System.Text.Encoding -> unit
Public Shared Sub AppendAllText (path As String, contents As String, encoding As Encoding)

パラメーター

path
String

指定した文字列の追加先となるファイル。

contents
String

ファイルに追加する文字列。

encoding
Encoding

使用する文字エンコーディング。

例外

.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: path は長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。

pathnullです。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

指定されたパスが有効ではありません (たとえば、ディレクトリが存在しない、またはマップされていないドライブにあるなど)。

ファイルを開くときに、I/O エラーが発生しました。

path が読み取り専用のファイルを指定しました。

- または -

この操作は、現在のプラットフォームではサポートされていません。

- または -

path がディレクトリを指定しました。

- または -

呼び出し元に、必要なアクセス許可がありません。

path の形式が正しくありません。

呼び出し元に、必要なアクセス許可がありません。

次のコード例では、 メソッドを使用してファイルの AppendAllText 末尾にテキストを追加する方法を示します。 この例では、ファイルがまだ存在しない場合に作成され、テキストが追加されます。 ただし、この例を正常に完了するには、ドライブ C に という名前 temp のディレクトリが存在する必要があります。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText, Encoding.UTF8);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
open System
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText =
        "Hello and Welcome" + Environment.NewLine

    File.WriteAllText(path, createText, Encoding.UTF8)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText, Encoding.UTF8)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim sw As StreamWriter

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText As String = "Hello and Welcome" + Environment.NewLine
            File.WriteAllText(path, createText, Encoding.UTF8)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText, Encoding.UTF8)

        ' Open the file to read from.
        Dim readText As String = File.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

注釈

文字列とファイル パスを指定すると、このメソッドは指定したファイルを開き、指定したエンコードを使用してファイルの末尾に文字列を追加してから、ファイルを閉じます。 例外が発生した場合でも、ファイル ハンドルはこのメソッドによって閉じられることが保証されます。

メソッドは、存在しない場合はファイルを作成しますが、新しいディレクトリは作成しません。 したがって、 パラメーターの値には既存のディレクトリが path 含まれている必要があります。

適用対象