發(fā)布時(shí)間:2017-08-22 08:27 作者:獨(dú)孤劍 閱讀:1083
C# 遞歸遍歷所有目錄,文件
// 調(diào)用 GetAllDirList("D:\\001"); static List<string> folderList = new List<string>(); /// <summary> /// 遞歸遍歷所有目錄 /// </summary> /// <param name="dir">傳入路徑</param> static void GetAllDirList(string dir) { DirectoryInfo di = new DirectoryInfo(dir); foreach (DirectoryInfo diNext in di.GetDirectories()) { // 添加目錄 folderList.Add(diNext.FullName); GetAllDirList(diNext.FullName); } } // 調(diào)用 GetAllFileList("D:\\001"); static List<string> fileList = new List<string>(); /// <summary> /// 遞歸遍歷所有文件 /// </summary> /// <param name="dir">傳入路徑</param> static void GetAllFileList(string dir) { DirectoryInfo di = new DirectoryInfo(dir); foreach (DirectoryInfo diNext in di.GetDirectories()) { // 遍歷目錄下的文件 foreach (FileInfo NextFile in diNext.GetFiles()) { // 標(biāo)記特殊文件 //if (NextFile.Name.Equals("data.mdb")) //{ // 添加文件 fileList.Add(diNext.FullName + "\\" + NextFile.Name); //} } GetAllFileList(diNext.FullName); } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對(duì)您有幫助,歡迎給作者打賞