C# 讀取大檔案
阿新 • • 發佈:2018-12-29
/// <summary> /// 讀取大檔案,每次讀取1M,優化可考慮分割讀取 /// </summary> /// <returns></returns> public static string ReadBinaryFileToString(FileStream fs) { if (fs != null) { string returnStr = ""; //儲存結果字串 try { byte[] byteArray = new byte[1024 * 1024];//開闢1M記憶體空間 BinaryReader reader = new BinaryReader(fs); while (reader.Read(byteArray, 0, byteArray.Length) > 0) { string content = AppTool.DEFAULT_FILE_ENCODING.GetString(byteArray); returnStr += content; } return returnStr; } catch (Exception e) { Debug.WriteLine(string.Format("讀取檔案出錯:訊息={0},堆疊={1}", e.Message, e.StackTrace)); } } return null; }