xxxxxxxxxx
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader("path/to/file.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
xxxxxxxxxx
using System.IO;
string filePath = "example.txt";
if (File.Exists(filePath))
{
using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// Do something with the line
Console.WriteLine(line);
}
}
}
xxxxxxxxxx
using System;
using System.IO;
public class Example
{
public static void Main()
{
string fileName = @"C:\example\path.txt";
using (StreamReader streamReader = File.OpenText(fileName))
{
string text = streamReader.ReadToEnd();
string[] lines = text.Split(Environment.NewLine);
foreach (string line in lines) {
Console.WriteLine(line);
}
}
}
}