StreamReader 读取Txt文件

淩亂°似流年 2021-11-23 15:16 409阅读 0赞

ContractedBlock.gif ExpandedBlockStart.gif Code

  1. 1 string filePath = @Environment.CurrentDirectory + "\\SimuAppClient.txt";
  2. 2 if(!File.Exists(filePath))
  3. 3 {
  4. 4 return;
  5. 5 }
  6. 6 var filest = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
  7. 7 using (var sr = new StreamReader(filest))
  8. 8 {
  9. 9 string strLine = sr.ReadLine(); //读取文件中的一行
  10. 10 while (strLine != null) //判断是否为空,表示到文件最后一行了
  11. 11 {
  12. 12 txtRead.AppendText(strLine
  13. 13 +"\r"); //自动换行
  14. 14 strLine = sr.ReadLine();
  15. 15 }
  16. 16 sr.Close(); //关闭流
  17. 17 filest.Close();
  18. 18 }

转载于:https://www.cnblogs.com/w2011/archive/2012/10/11/2719683.html

发表评论

表情:
评论列表 (有 0 条评论,409人围观)

还没有评论,来说两句吧...

相关阅读