1.获取指定文件夹下的指定后缀的文件信息
using Microsoft.IdentityModel.Protocols; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Async { public class Program { public static void Main(string[] args) { #region 获取指定文件夹下的指定后缀文件 string strPaths = ConfigurationManager.AppSettings["dirPath"]; List<FileInfo> lstfiles = GetFile(strPaths, ".jpg"); DirectoryInfo info = new DirectoryInfo(strPaths); foreach (var item in lstfiles) { var index = lstfiles.IndexOf(item); Console.WriteLine($"循环第{index}次 - 取到对应的文件信息:{item}"); } Console.ReadKey(); #endregion } #region 获取指定文件夹下的指定后缀文件 /// <summary> /// 获取文件夹下的莫个后缀的所有文件 /// </summary> /// <param name="path">文件路劲</param> /// <param name="ExtName">文件后缀</param> /// <returns></returns> public static List<FileInfo> GetFile(string path, string ExtName) { try { List<FileInfo> lst = new List<FileInfo>(); string[] dir = Directory.GetDirectories(path);// 文件夹列表 DirectoryInfo directoryInfo = new DirectoryInfo(path); FileInfo[] files = directoryInfo.GetFiles(); if (files.Length != 0 || dir.Length != 0) // 当前目录文件或文件夹不能为空 { foreach (FileInfo f in files) { if (ExtName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) { lst.Add(f); } } foreach (string d in dir) { GetFile(d, ExtName); } } return lst; } catch (Exception ex) { throw ex; } } #endregion } }
2.获取指定文件夹下的指定后缀的文件信息(包括子文件)
using Microsoft.IdentityModel.Protocols; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Async { public class Program { public static void Main(string[] args) { #region 获取指定文件夹下的指定后缀文件 string strPaths = ConfigurationManager.AppSettings["dirPath"]; List<FileInfo> lst = new List<FileInfo>(); List<FileInfo> lstfiles = GetFiles(strPaths, ".jpg", ref lst); DirectoryInfo info = new DirectoryInfo(strPaths); foreach (var item in lstfiles) { var index = lstfiles.IndexOf(item); Console.WriteLine($"循环第{index}次 - 取到对应的文件信息:{item}"); } Console.ReadKey(); #endregion } #region 获取指定文件夹下的指定后缀文件后缀(包括获取子文件下的) /// <summary> /// 获取指定文件夹下的指定后缀文件后缀(包括获取子文件下的) /// </summary> /// <param name="path">文件夹路径</param> /// <param name="ExtName">文件后缀</param> /// <param name="lst"></param> /// <returns></returns> public static List<FileInfo> GetFiles(string path, string ExtName, ref List<FileInfo> lst) { try { //List<FileInfo> lst = new List<FileInfo>(); string[] dir = Directory.GetDirectories(path);// 文件夹列表 DirectoryInfo directoryInfo = new DirectoryInfo(path); FileInfo[] files = directoryInfo.GetFiles(); if (files.Length != 0 || dir.Length != 0) // 当前目录文件或文件夹不能为空 { foreach (FileInfo f in files) { if (ExtName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) { lst.Add(f); } } foreach (string d in dir) { GetFiles(d, ExtName, ref lst); } } return lst; } catch (Exception ex) { throw ex; } } #endregion } }
3.将获取到的文件复制到另外的文件夹中
using Microsoft.IdentityModel.Protocols; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Async { public class Program { public static void Main(string[] args) { #region 获取指定文件夹下的指定后缀文件 string strPaths = ConfigurationManager.AppSettings["dirPath"]; string fidir = ConfigurationManager.AppSettings["Img"]; List<FileInfo> lstfiles = GetFile(strPaths, ".jpg"); DirectoryInfo info = new DirectoryInfo(strPaths); foreach (var item in lstfiles) { try { string fileName = item.Name; string despath = fidir + fileName; File.Copy(strPaths + item.ToString(), despath); Console.WriteLine(item); } catch (Exception) { throw; } } Console.WriteLine("复制完成!!"); Console.ReadKey(); #endregion } #region 获取指定文件夹下的指定后缀文件 /// <summary> /// 获取文件夹下的莫个后缀的所有文件 /// </summary> /// <param name="path">文件路劲</param> /// <param name="ExtName">文件后缀</param> /// <returns></returns> public static List<FileInfo> GetFile(string path, string ExtName) { try { List<FileInfo> lst = new List<FileInfo>(); string[] dir = Directory.GetDirectories(path);// 文件夹列表 DirectoryInfo directoryInfo = new DirectoryInfo(path); FileInfo[] files = directoryInfo.GetFiles(); if (files.Length != 0 || dir.Length != 0) // 当前目录文件或文件夹不能为空 { foreach (FileInfo f in files) { if (ExtName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) { lst.Add(f); } } foreach (string d in dir) { GetFile(d, ExtName); } } return lst; } catch (Exception ex) { throw ex; } } #endregion } }
4.App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup> <appSettings> <add key="dirPath" value="E:\1\"/> <add key="Img" value="D:\Img\"/> </appSettings> </configuration>