File.AppendText(String) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Crée un élément StreamWriter qui ajoute du texte encodé en UTF-8 à un fichier existant ou à un nouveau fichier si le fichier spécifié n'existe pas.
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
Paramètres
- path
- String
Chemin d'accès du fichier auquel le texte doit être ajouté.
Retours
Writer de flux qui ajoute du texte encodé en UTF-8 au fichier spécifié ou à un nouveau fichier.
Exceptions
L'appelant n'a pas l'autorisation requise.
.NET Framework et versions de .net Core antérieures à 2,1 : path
est une chaîne de longueur nulle, ne contient que des espaces blancs ou contient un ou plusieurs caractères non valides. Vous pouvez rechercher les caractères non valides à l’aide de la méthode GetInvalidPathChars().
path
a la valeur null
.
Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.
Le chemin spécifié est non valide (par exemple, le répertoire n’existe pas ou se trouve sur un lecteur non mappé).
path
est dans un format non valide.
Exemples
L’exemple suivant ajoute du texte à un fichier. La méthode crée un nouveau fichier si le fichier n’existe pas. Toutefois, le répertoire nommé temp
sur le lecteur C doit exister pour que l’exemple se termine correctement.
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
Remarques
Cette méthode est équivalente à la StreamWriter(String, Boolean) surcharge du constructeur. Si le fichier spécifié par path
n’existe pas, il est créé. Si le fichier existe, les opérations d’écriture sont écrites dans le StreamWriter texte ajouté au fichier. Des threads supplémentaires sont autorisés à lire le fichier lorsqu’il est ouvert.
Le path
paramètre est autorisé à spécifier des informations relatives au chemin d’accès relatif ou absolu. Les informations relatives au chemin d’accès relatif sont interprétées par rapport au répertoire de travail actuel. Pour obtenir le répertoire de travail actuel, consultez GetCurrentDirectory .
Le path
paramètre ne respecte pas la casse.
Pour obtenir la liste des tâches d’e/s courantes, consultez tâches d’e/s courantes.