[.net]如何讀取文字檔卻又不鎖定他,how to read text file without locking it

  • 75
  • 0

[.net]如何讀取文字檔卻又不鎖定他,how to read text file without locking it

public static IEnumerable<string> ReadLines(string path)
{
	using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 0x1000, FileOptions.SequentialScan))
	using (var sr = new StreamReader(fs, Encoding.UTF8))
	{
		string line;
		while ((line = sr.ReadLine()) != null)
		{
			yield return line;
		}
	}
}



參考資料:
c# - How can I read a text file without locking it? - Stack Overflow
工作經驗