using System; using System.Collections.Generic; using System.IO; using System.Linq;
//文件夹列表 private static List<string> DirectorysList = new List<string>(); //文件列表 private static List<string> FilesinfoList = new List<string>(); static void Main(string[] args) { //路径 例 D:\logs\2023-02-22 string path1 = Environment.CurrentDirectory + "\\Test1"; GetDirectoryFileList(path1); foreach (var itemFile in FilesinfoList) { //ShowSqlResult(itemFile);//输出所有文件及路径 } }
/// <summary> /// 获取一个文件夹下的所有文件和文件夹集合 /// </summary> /// <param name="path"></param> private static void GetDirectoryFileList(string path) { try { DirectoryInfo directory = new DirectoryInfo(path); FileSystemInfo[] filesArray = directory.GetFileSystemInfos(); foreach (var item in filesArray) { //是否是一个文件夹 if (item.Attributes == FileAttributes.Directory) { DirectorysList.Add(item.FullName); GetDirectoryFileList(item.FullName); } else { FilesinfoList.Add(item.FullName); } } } catch (Exception ex) { //MessageBox.Show("获取一个文件夹下的所有文件和文件夹集合 时出错!"+ex.ToString()); } }
标签:C#,List,System,item,文件夹,static,集合,using From: https://www.cnblogs.com/hailexuexi/p/17182830.html