//Usings.
using System;
using System.IO;
/// <summary>
/// Método para escribir una línea al final de un fichero txt.
/// </summary>
/// <param name="path">Ruta del fichero txt.</param>
/// <param name="text">Texto a añadir.</param>
public static void WriteFile(string path, string text)
{
try
{
// Abre el archivo en modo añadir.
using (StreamWriter writer = new StreamWriter(path, true))
{
// Escribe el texto al final del archivo.
writer.WriteLine(text);
}
}
catch (Exception ex)
{
Console.WriteLine("Excepción: " + ex.Message);
}
}
/// <summary>
/// Método para escribir una línea en un fichero txt, sobreescribiendo el contenido.
/// </summary>
/// <param name="path">Ruta del fichero txt.</param>
/// <param name="text">Texto a añadir.</param>
public static void ReWriteFile(string path, string text)
{
try
{
// Abre el archivo en modo sobreescribir.
using (StreamWriter writer = new StreamWriter(path, false))
{
// Escribe el texto en el archivo.
writer.WriteLine(text);
}
}
catch (Exception ex)
{
Console.WriteLine("Excepción: " + ex.Message);
}
}
C#: Escribir en un fichero .txt
Tiempo de lectura: < 1 minuto