File.AppendText(String) Metodo

Definizione

Crea un oggetto StreamWriter che aggiunge testo con codifica UTF-8 a un file esistente o a un nuovo file se quello specificato non esiste.

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

Parametri

path
String

Percorso del file al quale aggiungere testo.

Restituisce

Writer di flusso che aggiunge testo con codifica UTF-8 al file specificato o a un nuovo file.

Eccezioni

Il chiamante non dispone dell'autorizzazione richiesta.

.NET Framework e versioni di .NET Core precedenti alla 2.1: path è una stringa di lunghezza zero, contiene solo spazi vuoti o contiene uno o più caratteri non validi. È possibile cercare i caratteri non validi usando il metodo GetInvalidPathChars().

path è null.

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.

Il percorso specificato non è valido, ad esempio la directory non esiste o si trova in un'unità non mappata.

Il formato di path non è valido.

Esempio

Nell'esempio seguente viene aggiunto testo a un file. Il metodo crea un nuovo file se il file non esiste. Tuttavia, la directory denominata temp nell'unità C deve esistere perché l'esempio venga completato correttamente.

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);
            }
        }
    }
}
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

Commenti

Questo metodo equivale all'overload del StreamWriter(String, Boolean) costruttore. Se il file specificato da path non esiste, viene creato. Se il file esiste, scrivere operazioni nel StreamWriter testo di accodamento al file. I thread aggiuntivi sono autorizzati a leggere il file mentre è aperto.

Il path parametro è autorizzato a specificare informazioni relative o assolute sul percorso. Le informazioni relative sul percorso sono interpretate come relative alla directory di lavoro corrente. Per ottenere la directory di lavoro corrente, vedere GetCurrentDirectory.

Il path parametro non fa distinzione tra maiuscole e minuscole.

Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Si applica a

Vedi anche