C# 程式一次讀取檔案的所有行


使用 ReadAllText() 方法一次讀取檔案的所有行。

假設我們有一個檔案 “hello.txt”,其中包含以下行 −

One
Two
Three

要讀取上述檔案,請將檔案路徑作為引數新增。

File.ReadAllText(myPath);

上面,myPath 包含檔案路徑。

String myPath = "hello.txt";

讓我們看看完整的程式碼 −

示例

using System;
using System.IO;
public class Demo {
   public static void Main() {
      String myPath = "hello.txt";
      String allLines;
      allLines = File.ReadAllText(myPath);
      Console.WriteLine(allLines);
   }
}

輸出

以下是輸出 −

One
Two
Three

152 次瀏覽

提升您的 職業生涯

完成課程獲得認證

開始
廣告