using System;
using System.IO;
namespace Utilerias
{
public class Escritor
{
public static void EscribirEnArchivo(string archivo, string texto, bool sobreescribirArchivo)
{
TextWriter escritor = null;
try
{
escritor = (sobreescribirArchivo) ?
File.CreateText(archivo) :
File.AppendText(archivo);
escritor.Write(texto);
}
catch (Exception exc)
{
throw exc;
}
finally
{
if (escritor != null)
{
escritor.Close();
escritor.Dispose();
}
}
}
}
}
Para usar la clase se utiliza:
using System;
using Utilerias;
namespace EscritorEnTextoPrueba
{
class Program
{
static void Main(string[] args)
{
try
{
Escritor.EscribirEnArchivo(@"D:\directorio\archivo.txt",
"Este es un texto de prueba\r\n"
+ "Este es un texto de prueba\r\n"
+ "Este es un texto de prueba\r\n"
+ "Este es un texto de prueba\r\n"
+ "Este es un texto de prueba\r\n"
+ "Este es un texto de prueba\r\n", false);
Console.Write("Archivo creado correctamente");
}
catch (Exception exc)
{
Console.Write(exc.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
No hay comentarios:
Publicar un comentario