File.AppendText(String) メソッド

定義

StreamWriter を作成します。これは、UTF-8 でエンコードされたテキストを既存のファイルに 追加するか、指定したファイルが存在しない場合は新しいファイルに追加します。

public:
 static System::IO::StreamWriter ^ AppendText(System::String ^ path);
public static System.IO.StreamWriter AppendText (string path);
static member AppendText : string -> System.IO.StreamWriter
Public Shared Function AppendText (path As String) As StreamWriter

パラメーター

path
String

追加先のファイル パス。

戻り値

StreamWriter

指定したファイルまたは新しいファイルに、UTF-8 エンコードされたテキストを追加するストリーム ライター。

例外

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

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

pathnullです。

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

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

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

次の例では、ファイルにテキストを追加します。 このメソッドは、ファイルが存在しない場合に新しいファイルを作成します。 ただし、この例を正常に完了するには、ドライブ C に名前が付いた temp ディレクトリが存在する必要があります。

using namespace System;
using namespace System::IO;

int 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.
      StreamWriter^ sw = File::CreateText( path );
      try
      {
         sw->WriteLine( "Hello" );
         sw->WriteLine( "And" );
         sw->WriteLine( "Welcome" );
      }
      finally
      {
         if ( sw )
            delete (IDisposable^)sw;
      }
   }
   
   // This text is always added, making the file longer over time
   // if it is not deleted.
   StreamWriter^ sw = File::AppendText( path );
   try
   {
      sw->WriteLine( "This" );
      sw->WriteLine( "is Extra" );
      sw->WriteLine( "Text" );
   }
   finally
   {
      if ( sw )
         delete (IDisposable^)sw;
   }
   
   // Open the file to read from.
   StreamReader^ sr = File::OpenText( path );
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
         delete (IDisposable^)sr;
   }
}
using System;
using System.IO;

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.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }	
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        using (StreamWriter sw = File.AppendText(path))
        {
            sw.WriteLine("This");
            sw.WriteLine("is Extra");
            sw.WriteLine("Text");
        }	

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}
Imports System.IO

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 Not File.Exists(path) Then
      ' Create a file to write to.
      Using sw As StreamWriter = File.CreateText(path)
        sw.WriteLine("Hello")
        sw.WriteLine("And")
        sw.WriteLine("Welcome")
      End Using
    End If

    ' This text is always added, making the file longer over time 
    ' if it is not deleted.
    Using sw As StreamWriter = File.AppendText(path)
      sw.WriteLine("This")
      sw.WriteLine("is Extra")
      sw.WriteLine("Text")
    End Using

    ' Open the file to read from. 
    Using sr As StreamReader = File.OpenText(path)
      Do While sr.Peek() >= 0
        Console.WriteLine(sr.ReadLine())
      Loop
    End Using

  End Sub
End Class

注釈

このメソッドは、コンストラクターのオーバーロードと StreamWriter(String, Boolean) 同じです。 指定された path ファイルが存在しない場合は作成されます。 ファイルが存在する場合は、ファイルの追加テキストに対する StreamWriter 書き込み操作を行います。 開いている間は、追加のスレッドでファイルの読み取りが許可されます。

この path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、次を参照してください GetCurrentDirectory

パラメーターでは path 大文字と小文字は区別されません。

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

適用対象

こちらもご覧ください