一、File与Directory
二、读取文件(StreamReader)
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //从指定盘下读取.txt文件 6 string sdick=@"C:"; 7 string[] fileExtension={".txt"}; 8 if (Directory.Exists(sdick)) 9 { 10 string[] filePath=Directory.GetFiles(sdick); 11 if (filePath.Count() > 0) 12 { 13 Console.WriteLine(string.Format("共获取到{0}个文件:", filePath.Count())); 14 15 List<FileInfo> lstFiles = new List<FileInfo>(); 16 foreach (string s in filePath) 17 { 18 FileInfo fi = new FileInfo(s); 19 Console.WriteLine(string.Format(" 文件:{0},后缀为:{1}", fi.Name,fi.Extension)); 20 21 if (fileExtension.Contains(fi.Extension)) 22 { 23 lstFiles.Add(fi); 24 } 25 } 26 27 string condition=""; 28 fileExtension.All((file)=>{ 29 condition+="["+file+"]"; 30 return true; 31 }); 32 33 Console.WriteLine(string.Format("查找到满足条件:{0}的文件有{1}个。", condition, lstFiles.Count)); 34 35 //读取文件 36 if(lstFiles.Count>0) 37 { 38 foreach (FileInfo fi in lstFiles) 39 { 40 Console.WriteLine(string.Format("文件【{0}】的内容如下:", fi.Name)); 41 Console.WriteLine(new string('-', 20)); 42 43 using (StreamReader sr = new StreamReader(fi.FullName)) 44 { 45 Console.WriteLine(sr.ReadToEnd()); 46 } 47 48 Console.WriteLine(" "); 49 } 50 } 51 52 53 54 } 55 } 56 57 58 59 Console.ReadLine(); 60 } 61 }