// 名稱空間
using System.IO;
1、一次性讀取所有行:
string [] _lines = File.ReadAllText("Txt檔案路徑");
2、按行讀取所有行:
using (FileStream fileStream = new FileStream("Txt檔案路徑", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (fileStream.CanRead)
StreamReader streamReader = new StreamReader(fileStream, System.Text.Encoding.Default);
string _line = streamReader.ReadLine();
while (_line != null)
_line = streamReader.ReadLine();
}
streamReader.Close();
// 名稱空間
using System.IO;
1、一次性讀取所有行:
string [] _lines = File.ReadAllText("Txt檔案路徑");
2、按行讀取所有行:
using (FileStream fileStream = new FileStream("Txt檔案路徑", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (fileStream.CanRead)
{
StreamReader streamReader = new StreamReader(fileStream, System.Text.Encoding.Default);
string _line = streamReader.ReadLine();
while (_line != null)
{
_line = streamReader.ReadLine();
}
streamReader.Close();
}
}