File.AppendText(String) 方法

定義

建立會將 UTF-8 編碼的文字附加至現有檔案或新檔案 (如果指定的檔案不存在) 的 StreamWriterCreates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.

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

要附加至檔案的路徑。The path to the file to append to.

傳回

資料流寫入器,會附加 UTF-8 編碼的文字至指定的檔案或新檔案。A stream writer that appends UTF-8 encoded text to the specified file or to a new file.

例外狀況

呼叫端沒有必要的權限。The caller does not have the required permission.

path 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

pathnullpath is null.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

指定的路徑無效 (例如,此目錄不存在或位於未對應的磁碟機上)。The specified path is invalid (for example, the directory doesn't exist or it is on an unmapped drive).

path 格式無效。path is in an invalid format.

範例

下列範例會將文字附加至檔案。The following example appends text to a file. 如果檔案不存在,方法會建立新的檔案。The method creates a new file if the file doesn't exist. 不過,C 磁片磁碟機上名為 temp 的目錄必須存在,範例才能順利完成。However, the directory named temp on drive C must exist for the example to complete successfully.

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) 的函數多載。This method is equivalent to the StreamWriter(String, Boolean) constructor overload. 如果 path 所指定的檔案不存在,則會建立該檔案。If the file specified by path does not exist, it is created. 如果檔案存在,則將作業寫入 StreamWriter 將文字附加至檔案。If the file does exist, write operations to the StreamWriter append text to the file. 在檔案開啟時,允許額外的執行緒讀取檔案。Additional threads are permitted to read the file while it is open.

path 參數允許指定相對或絕對路徑資訊。The path parameter is permitted to specify relative or absolute path information. 相對路徑資訊會解讀為相對於目前工作目錄。Relative path information is interpreted as relative to the current working directory. 若要取得目前的工作目錄,請參閱 GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

path 參數不區分大小寫。The path parameter is not case-sensitive.

如需一般 i/o 工作的清單,請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
用於附加至指定的檔案。for appending to the specified file. 相關聯的列舉: AppendAssociated enumeration: Append

適用於

另請參閱