File.AppendText(String) Metoda

Definice

StreamWriter Vytvoří, který připojí uTF-8 kódovaný text k existujícímu souboru nebo k novému souboru, pokud zadaný soubor neexistuje.

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

Parametry

path
String

Cesta k souboru, ke které se má připojit.

Návraty

Zapisovač datového proudu, který připojí kódovaný text UTF-8 k zadanému souboru nebo k novému souboru.

Výjimky

Volající nemá požadované oprávnění.

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Pomocí metody můžete zadat dotaz na neplatné znaky GetInvalidPathChars() .

path je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

Zadaná cesta je neplatná (například adresář neexistuje nebo je na nemapované jednotce).

path je v neplatném formátu.

Příklady

Následující příklad připojí text k souboru. Metoda vytvoří nový soubor, pokud soubor neexistuje. Aby se však tento příklad úspěšně dokončil, musí existovat adresář pojmenovaný temp na jednotce C.

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

Poznámky

Tato metoda je ekvivalentní přetížení konstruktoru StreamWriter(String, Boolean) . Pokud soubor zadaný path pomocí neexistuje, vytvoří se. Pokud soubor existuje, operace zápisu do připojeného StreamWriter textu k souboru. Další vlákna jsou povolena ke čtení souboru, když je otevřená.

Parametr path má povoleno zadat relativní nebo absolutní informace o cestě. Relativní informace o cestě se interpretují jako relativní vzhledem k aktuálnímu pracovnímu adresáři. Aktuální pracovní adresář získáte v tématu GetCurrentDirectory.

Parametr path nerozlišuje malá a velká písmena.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Platí pro

Viz také