File.AppendText(String) Methode

Definitie

Hiermee maakt u een StreamWriter tekst die UTF-8-gecodeerde tekst toevoegt aan een bestaand bestand of aan een nieuw bestand als het opgegeven bestand niet bestaat.

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

Parameters

path
String

Het pad naar het bestand waaraan u wilt toevoegen.

Retouren

Een stroomschrijver die UTF-8 gecodeerde tekst toevoegt aan het opgegeven bestand of aan een nieuw bestand.

Uitzonderingen

De beller heeft niet de vereiste machtiging.

.NET Framework en .NET Core-versies ouder dan 2.1: path is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens. U kunt een query uitvoeren op ongeldige tekens met behulp van de GetInvalidPathChars() methode.

path is null.

Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.

Het opgegeven pad is ongeldig (de map bestaat bijvoorbeeld niet of bevindt zich op een niet-toegewezen station).

path heeft een ongeldige indeling.

Voorbeelden

In het volgende voorbeeld wordt tekst aan een bestand toegevoegd. Met de methode wordt een nieuw bestand gemaakt als het bestand niet bestaat. De map met de naam op temp station C moet echter bestaan om het voorbeeld te voltooien.

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

Opmerkingen

Deze methode is gelijk aan de overbelasting van de StreamWriter(String, Boolean) constructor. Als het bestand dat is opgegeven path niet bestaat, wordt het gemaakt. Als het bestand bestaat, worden schrijfbewerkingen naar de StreamWriter toevoegtekst aan het bestand geschreven. Aanvullende threads mogen het bestand lezen terwijl het is geopend.

De path parameter mag relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap. Als u de huidige werkmap wilt ophalen, raadpleegt GetCurrentDirectoryu .

De path parameter is niet hoofdlettergevoelig.

Zie Algemene I/O-taken voor een lijst met algemene I/O-taken.

Van toepassing op

Zie ook