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.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 = GetFilevill(strPaths, ref lst); DirectoryInfo info = new DirectoryInfo(strPaths); foreach (var item in lstfiles) { Console.WriteLine(item); } Console.WriteLine($"当前获取到的文件总数:{GetFilesCount(info)}"); Console.ReadKey(); #endregion } #region 获取指定文件夹下的所有文件信息(包括获取子文件下的) /// <summary> /// 获取指定文件夹下的所有文件信息(包括获取子文件下的) /// </summary> /// <param name="path">文件夹路径</param> /// <param name="lst"></param> /// <returns></returns> public static List<FileInfo> GetFilevill(string path, 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) { lst.Add(f); } foreach (string d in dir) { GetFilevill(d, ref lst); } } return lst; } catch (Exception ex) { throw ex; } } #endregion #region 获取当前目录下的文件总数 /// <summary> /// 获取文件夹下的文件总数 /// </summary> /// <param name="dirInfo"></param> /// <returns></returns> public static int GetFilesCount(DirectoryInfo dirInfo) { int totalFile = 0; totalFile += dirInfo.GetFiles().Length; foreach (DirectoryInfo f in dirInfo.GetDirectories()) { totalFile += GetFilesCount(f); } return totalFile; } #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> lstfiles = GetFilevill(strPaths); DirectoryInfo info = new DirectoryInfo(strPaths); foreach (FileInfo item in lstfiles) { Console.WriteLine(item); } Console.WriteLine($"当前获取到的文件总数:{GetFilesCount(info)}"); Console.ReadKey(); #endregion } #region 获取指定文件夹下的所有文件信息(包括获取子文件下的) /// <summary> /// 获取指定文件夹下的所有文件信息(包括获取子文件下的) /// </summary> /// <param name="path">文件夹路径</param> /// <param name="lst"></param> /// <returns></returns> public static List<FileInfo> GetFilevill(string path) { try { List<FileInfo> lsting = 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) { lsting.Add(f); } } return lsting; } catch (Exception ex) { throw ex; } } #endregion #region 获取当前目录下的文件总数 /// <summary> /// 获取文件夹下的文件总数 /// </summary> /// <param name="dirInfo"></param> /// <returns></returns> public static int GetFilesCount(DirectoryInfo dirInfo) { int totalFile = 0; totalFile += dirInfo.GetFiles().Length; foreach (DirectoryInfo f in dirInfo.GetDirectories()) { totalFile += GetFilesCount(f); } return totalFile; } #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"]; List<FileInfo> lstfiles = GetFilevill(strPaths); DirectoryInfo info = new DirectoryInfo(strPaths); foreach (FileInfo item in lstfiles) { Console.WriteLine(item); } Console.WriteLine($"当前获取到的文件总数:{GetFilesCount(info)}"); Console.WriteLine("--异步--"); Thread.Sleep(3000); Task task = StepFile(); Console.WriteLine($"当前获取到的文件总数:{GetFilesCount(info)}"); Console.ReadKey(); #endregion } #region 采用异步 public static async Task StepFile() { try { await Task.Run(() => { Console.WriteLine("--获取文件夹下的所有文件(包括子文件夹)--"); // 图片存储的文件夹路径 string strPath = ConfigurationManager.AppSettings["dirPath"]; DirectoryInfo info = new DirectoryInfo(strPath); List<FileInfo> lstfile = GetFilevill(strPath); //文件复制后保存的文件夹,读取App.config配置文件地址 string fidir = ConfigurationManager.AppSettings["Img"]; foreach (FileInfo item in lstfile) { try { string fileName = item.Name; string despath = fidir + fileName; File.Copy(strPath + item.ToString(), despath); Console.WriteLine(item); } catch (Exception ex) { throw ex; } } Console.WriteLine("复制文件成功"); Console.WriteLine($"当前获取到的文件总数:{GetFilesCount(info)}"); }); } catch (Exception ex) { throw ex; } } #endregion #region 获取指定文件夹下的所有文件信息(包括获取子文件下的) /// <summary> /// 获取指定文件夹下的所有文件信息(包括获取子文件下的) /// </summary> /// <param name="path">文件夹路径</param> /// <param name="lst"></param> /// <returns></returns> public static List<FileInfo> GetFilevill(string path) { try { List<FileInfo> lsting = 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) { lsting.Add(f); } } return lsting; } catch (Exception ex) { throw ex; } } #endregion #region 获取当前目录下的文件总数 /// <summary> /// 获取文件夹下的文件总数 /// </summary> /// <param name="dirInfo"></param> /// <returns></returns> public static int GetFilesCount(DirectoryInfo dirInfo) { int totalFile = 0; totalFile += dirInfo.GetFiles().Length; foreach (DirectoryInfo f in dirInfo.GetDirectories()) { totalFile += GetFilesCount(f); } return totalFile; } #endregion } }
4.将获取到的信息文件复制到另外一个文件夹(包括子文件夹)??
5.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>