File.AppendText(String) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
UTF-8 でエンコードされたテキストを既存のファイルに追加する StreamWriter を作成します。指定したファイルが存在しない場合は新しいファイルに追加します。
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
追加するファイルのパス。
返品
指定したファイルまたは新しいファイルに UTF-8 でエンコードされたテキストを追加するストリーム ライター。
例外
呼び出し元に必要なアクセス許可がありません。
2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。
GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。
path は nullです。
指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。
指定したパスが無効です (たとえば、ディレクトリが存在しないか、マップされていないドライブ上にあります)。
path が無効な形式です。
例
次の例では、ファイルにテキストを追加します。 このメソッドは、ファイルが存在しない場合に新しいファイルを作成します。 ただし、この例が正常に完了するには、ドライブ C の temp という名前のディレクトリが存在する必要があります。
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);
}
}
}
}
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.
use 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.
do
use sw = File.AppendText path
sw.WriteLine "This"
sw.WriteLine "is Extra"
sw.WriteLine "Text"
// Open the file to read from.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()
while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
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 タスク」を参照してください。