C#获取指定文件夹下指定后辍文件信息
转自:c#下获取当前文件夹下指定后缀文件信息(三) - ArSang-Blog - 博客园 (cnblogs.com)
1.获取指定文件夹下的指定后缀的文件信息
1 using Microsoft.IdentityModel.Protocols; 2 using System; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.IO; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 11 namespace Async 12 { 13 public class Program 14 { 15 16 public static void Main(string[] args) 17 { 18 #region 获取指定文件夹下的指定后缀文件 19 string strPaths = ConfigurationManager.AppSettings["dirPath"]; 20 List<FileInfo> lstfiles = GetFile(strPaths, ".jpg"); 21 DirectoryInfo info = new DirectoryInfo(strPaths); 22 foreach (var item in lstfiles) 23 { 24 var index = lstfiles.IndexOf(item); 25 Console.WriteLine($"循环第{index}次 - 取到对应的文件信息:{item}"); 26 } 27 28 Console.ReadKey(); 29 #endregion 30 } 31 32 #region 获取指定文件夹下的指定后缀文件 33 34 /// <summary> 35 /// 获取文件夹下的莫个后缀的所有文件 36 /// </summary> 37 /// <param name="path">文件路劲</param> 38 /// <param name="ExtName">文件后缀</param> 39 /// <returns></returns> 40 public static List<FileInfo> GetFile(string path, string ExtName) 41 { 42 43 try 44 { 45 List<FileInfo> lst = new List<FileInfo>(); 46 string[] dir = Directory.GetDirectories(path);// 文件夹列表 47 DirectoryInfo directoryInfo = new DirectoryInfo(path); 48 FileInfo[] files = directoryInfo.GetFiles(); 49 if (files.Length != 0 || dir.Length != 0) // 当前目录文件或文件夹不能为空 50 { 51 foreach (FileInfo f in files) 52 { 53 if (ExtName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) 54 { 55 lst.Add(f); 56 } 57 } 58 foreach (string d in dir) 59 { 60 GetFile(d, ExtName); 61 } 62 } 63 return lst; 64 } 65 catch (Exception ex) 66 { 67 68 throw ex; 69 70 } 71 } 72 #endregion 73 74 } 75 }
2.获取指定文件夹下的指定后缀的文件信息(包括子文件)
1 using Microsoft.IdentityModel.Protocols; 2 using System; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.IO; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 11 namespace Async 12 { 13 public class Program 14 { 15 16 public static void Main(string[] args) 17 { 18 #region 获取指定文件夹下的指定后缀文件 19 string strPaths = ConfigurationManager.AppSettings["dirPath"]; 20 List<FileInfo> lst = new List<FileInfo>(); 21 List<FileInfo> lstfiles = GetFiles(strPaths, ".jpg", ref lst); 22 DirectoryInfo info = new DirectoryInfo(strPaths); 23 foreach (var item in lstfiles) 24 { 25 var index = lstfiles.IndexOf(item); 26 Console.WriteLine($"循环第{index}次 - 取到对应的文件信息:{item}"); 27 } 28 29 Console.ReadKey(); 30 #endregion 31 } 32 33 #region 获取指定文件夹下的指定后缀文件后缀(包括获取子文件下的) 34 35 /// <summary> 36 /// 获取指定文件夹下的指定后缀文件后缀(包括获取子文件下的) 37 /// </summary> 38 /// <param name="path">文件夹路径</param> 39 /// <param name="ExtName">文件后缀</param> 40 /// <param name="lst"></param> 41 /// <returns></returns> 42 public static List<FileInfo> GetFiles(string path, string ExtName, ref List<FileInfo> lst) 43 { 44 45 try 46 { 47 //List<FileInfo> lst = new List<FileInfo>(); 48 string[] dir = Directory.GetDirectories(path);// 文件夹列表 49 DirectoryInfo directoryInfo = new DirectoryInfo(path); 50 FileInfo[] files = directoryInfo.GetFiles(); 51 if (files.Length != 0 || dir.Length != 0) // 当前目录文件或文件夹不能为空 52 { 53 foreach (FileInfo f in files) 54 { 55 if (ExtName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) 56 { 57 lst.Add(f); 58 } 59 } 60 foreach (string d in dir) 61 { 62 GetFiles(d, ExtName, ref lst); 63 } 64 } 65 return lst; 66 } 67 catch (Exception ex) 68 { 69 70 throw ex; 71 } 72 } 73 #endregion 74 75 } 76 }
3.将获取到的文件复制到另外的文件夹中
1 using Microsoft.IdentityModel.Protocols; 2 using System; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.IO; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 11 namespace Async 12 { 13 public class Program 14 { 15 16 public static void Main(string[] args) 17 { 18 #region 获取指定文件夹下的指定后缀文件 19 string strPaths = ConfigurationManager.AppSettings["dirPath"]; 20 string fidir = ConfigurationManager.AppSettings["Img"]; 21 List<FileInfo> lstfiles = GetFile(strPaths, ".jpg"); 22 DirectoryInfo info = new DirectoryInfo(strPaths); 23 foreach (var item in lstfiles) 24 { 25 try 26 { 27 string fileName = item.Name; 28 string despath = fidir + fileName; 29 File.Copy(strPaths + item.ToString(), despath); 30 Console.WriteLine(item); 31 } 32 catch (Exception) 33 { 34 35 throw; 36 } 37 } 38 Console.WriteLine("复制完成!!"); 39 Console.ReadKey(); 40 #endregion 41 } 42 43 #region 获取指定文件夹下的指定后缀文件 44 45 /// <summary> 46 /// 获取文件夹下的莫个后缀的所有文件 47 /// </summary> 48 /// <param name="path">文件路劲</param> 49 /// <param name="ExtName">文件后缀</param> 50 /// <returns></returns> 51 public static List<FileInfo> GetFile(string path, string ExtName) 52 { 53 54 try 55 { 56 List<FileInfo> lst = new List<FileInfo>(); 57 string[] dir = Directory.GetDirectories(path);// 文件夹列表 58 DirectoryInfo directoryInfo = new DirectoryInfo(path); 59 FileInfo[] files = directoryInfo.GetFiles(); 60 if (files.Length != 0 || dir.Length != 0) // 当前目录文件或文件夹不能为空 61 { 62 foreach (FileInfo f in files) 63 { 64 if (ExtName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) 65 { 66 lst.Add(f); 67 } 68 } 69 foreach (string d in dir) 70 { 71 GetFile(d, ExtName); 72 } 73 } 74 return lst; 75 } 76 catch (Exception ex) 77 { 78 79 throw ex; 80 81 } 82 } 83 #endregion 84 85 } 86 }
4.App.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> 5 </startup> 6 <appSettings> 7 <add key="dirPath" value="E:\1\"/> 8 <add key="Img" value="D:\Img\"/> 9 </appSettings> 10 </configuration>