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);
}
}
}
}
xxxxxxxxxx
string inputFilePath = @"C:\input.txt";
string outputFilePath = @"C:\output.txt";
// Read the input file line by line
using (StreamReader reader = new StreamReader(inputFilePath))
{
using (StreamWriter writer = new StreamWriter(outputFilePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// Write the line to the output file
writer.WriteLine(line);
}
}
}
xxxxxxxxxx
using (var file = new StreamReader(fileName)) {
string line;
while((line = file.ReadLine()) != null)
{
System.Console.WriteLine(line);
}
}
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
string GetLine(string fileName, int line)
{
using (var sr = new StreamReader(fileName)) {
for (int i = 1; i < line; i++)
sr.ReadLine();
return sr.ReadLine();
}
}