File.AppendText 方法

创建一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function AppendText ( _
    path As String _
) As StreamWriter
用法
Dim path As String
Dim returnValue As StreamWriter

returnValue = File.AppendText(path)
public static StreamWriter AppendText (
    string path
)
public:
static StreamWriter^ AppendText (
    String^ path
)
public static StreamWriter AppendText (
    String path
)
public static function AppendText (
    path : String
) : StreamWriter

参数

  • path
    要向其中追加内容的文件的路径。

返回值

一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件。

异常

异常类型 条件

UnauthorizedAccessException

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

ArgumentException

path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。

ArgumentNullException

path 为 空引用(在 Visual Basic 中为 Nothing)。

PathTooLongException

指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。

DirectoryNotFoundException

指定的路径无效(例如,它位于未映射的驱动器上)。

NotSupportedException

path 的格式无效。

备注

此方法等效于 StreamWriter(String,Boolean) 构造函数重载。如果由 path 指定的文件不存在,则创建该文件。如果该文件存在,则对 StreamWriter 的写入操作将文本追加到该文件。允许其他线程在文件打开后读取该文件。

允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory

path 参数不区分大小写。

有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

示例

下面的示例将文本追加到文件。

Imports System
Imports System.IO

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.
            sw = File.CreateText(path)

            sw.WriteLine("Hello")
            sw.WriteLine("And")
            sw.WriteLine("Welcome")
            sw.Flush()
            sw.Close()
        End If

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

        ' Open the file to read from.
        Dim sr As StreamReader = File.OpenText(path)
        Dim s As String
        Do While sr.Peek() >= 0
            s = sr.ReadLine()
            Console.WriteLine(s)
        Loop
        sr.Close()
    End Sub
End Class
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);
            }
        }
    }
}
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;
   }
}
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        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 {
                sw.Dispose();
            }
        }

        // 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 {
            sw.Dispose();
        }

        // Open the file to read from.
        StreamReader sr = File.OpenText(path);
        try {
            String s = "";
            while ((s = sr.ReadLine())!= null) {
                Console.WriteLine(s);
            }
        }
        finally {
            sr.Dispose();
        }
    } //main
} //Test

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

File 类
File 成员
System.IO 命名空间
StreamWriter

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本
基本的文件 I/O
如何:对新建的数据文件进行读取和写入