FileInfo.AppendText (Método)
Crea un objeto StreamWriter que agrega texto al archivo representado por esta instancia de FileInfo.
Espacio de nombres: System.IO
Ensamblado: mscorlib (en mscorlib.dll)
Ensamblado: mscorlib (en mscorlib.dll)
En el siguiente ejemplo se anexa texto a un archivo y se lee el archivo.
using System; using System.IO; class Test { public static void Main() { FileInfo fi = new FileInfo(@"c:\temp\MyTest.txt"); // This text is added only once to the file. if (!fi.Exists) { //Create a file to write to. using (StreamWriter sw = fi.CreateText()) { sw.WriteLine("Hello"); sw.WriteLine("And"); sw.WriteLine("Welcome"); } } // This text will always be added, making the file longer over time // if it is not deleted. using (StreamWriter sw = fi.AppendText()) { sw.WriteLine("This"); sw.WriteLine("is Extra"); sw.WriteLine("Text"); } //Open the file to read from. using (StreamReader sr = fi.OpenText()) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } }
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
FileInfo fi = new FileInfo("c:\\temp\\MyTest.txt");
// This text is added only once to the file.
if (!(fi.get_Exists())) {
//Create a file to write to.
StreamWriter sw = fi.CreateText();
try {
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
finally {
sw.Dispose();
}
}
// This text will always be added, making the file longer over time
// if it is not deleted.
StreamWriter sw = fi.AppendText();
try {
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
finally {
sw.Dispose();
}
//Open the file to read from.
StreamReader sr = fi.OpenText();
try {
String s = "";
while ((s = sr.ReadLine()) != null) {
Console.WriteLine(s);
}
}
finally {
sr.Dispose();
}
} //main
} //Test
En el siguiente ejemplo se muestra cómo anexar texto al final de un archivo y se muestra también el resultado de la operación de anexado en la consola. La primera vez que se llama a esta rutina, se crea el archivo si no existe. A continuación, se anexa el texto especificado al archivo.
using System; using System.IO; public class AppendTextTest { public static void Main() { // Create a reference to a file, which might or might not exist. // If it does not exist, it is not yet created. FileInfo fi = new FileInfo("temp.txt"); // Create a writer, ready to add entries to the file. StreamWriter sw = fi.AppendText(); sw.WriteLine("Add as many lines as you like..."); sw.WriteLine("Add another line to the output..."); sw.Flush(); sw.Close(); // Get the information out of the file and display it. // Remember that the file might have other lines if it already existed. StreamReader sr = new StreamReader(fi.OpenRead()); while (sr.Peek() != -1) Console.WriteLine( sr.ReadLine() ); } }
- FileIOPermission para leer archivos y anexarles texto. Enumeraciones asociadas: FileIOPermissionAccess.Read, FileIOPermissionAccess.Append.
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.
Contenido de la comunidad
Agregar