跳至主要内容

👩‍💻 讀文檔 逐行讀取

1. File.ReadAllLines

File.ReadAllLines(FileName);

2. ReadLine()

//We have to create Streader Object to use this method
StreamReader ObjectName = new StreamReader(FileName);
ObjectName.ReadLine();
using System;
using System.IO;

public class ReadFile
{
public static void Main()
{
string FileToRead = @"D:\New folder\textFile.txt";
using (StreamReader ReaderObject = new StreamReader(FileToRead))
{
string Line;
// ReaderObject reads a single line, stores it in Line string variable and then displays it on console
while((Line = ReaderObject.ReadLine()) != null)
{
Console.WriteLine(Line);
}
}

}
}

ref: C# 逐行讀取文字檔案 | D棧 - Delft Stack