1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace StreamReaderTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string path = @"C:\Windows\System32\drivers\etc\hosts";
  13.             string myline = "";
  14.             if (File.Exists(path))
  15.             {
  16.                 //using(){}自动帮助我们释放流所占用的空间
  17.                 //()创建过程
  18.                 //{}读取或写入的过程
  19.                 //均不能加分号
  20.  
  21.                 using (StreamReader sr = new StreamReader(path))
  22.                 {
  23.                     myline = sr.ReadLine();
  24.                     while (myline != null)
  25.                     {
  26.                         Console.WriteLine(myline);
  27.                         myline = sr.ReadLine();
  28.                     }
  29.                 }
  30.             }
  31.             else
  32.             {
  33.                 Console.WriteLine("文件不存在!");
  34.             }
  35.             Console.ReadLine();
  36.             //方法一:
  37.  
  38.             //方法二:
  39.         }
  40.     }
  41. }
C#编程-119:读取文件StreamReader类_彭世瑜_新浪博客_.net

C#编程-119:读取文件StreamReader类_彭世瑜_新浪博客_.net_02

C#编程-119:读取文件StreamReader类_彭世瑜_新浪博客_.net_03