//Usings.
using System
using System.IO;
/// <summary>
/// Método para leer el contenido de un fichero txt y mostrarlo en consola.
/// </summary>
/// <param name="path">Ruta del fichero txt.</param>
public static void ReadFile(string path)
{
try
{
// Verificar si el archivo existe.
if (File.Exists(path))
{
// Leer el contenido del archivo.
using (StreamReader reader = new StreamReader(path))
{
string contenido = reader.ReadToEnd();
Console.WriteLine("Contenido del archivo:");
Console.WriteLine(contenido);
}
}
else
{
Console.WriteLine("El archivo no existe.");
}
}
catch (Exception ex)
{
Console.WriteLine("Excepción: " + ex.Message);
}
}
C#: Leer un fichero .txt
Tiempo de lectura: < 1 minuto