參考:連接忘記了。。。。
#region 查找目錄下包含子目錄的全部文件 /// <summary> /// 獲得目錄下所有文件或指定文件類型文件地址 /// </summary> public static List<string> fileList = new List<string>(); public static string[] GetFiles(string fullPath, string extName, bool isFullName = false) { try { fileList.Clear(); DirectoryInfo dirs = new DirectoryInfo(fullPath); //獲得程序所在路徑的目錄對象 DirectoryInfo[] dir = dirs.GetDirectories();//獲得目錄下文件夾對象 FileInfo[] file = dirs.GetFiles();//獲得目錄下文件對象 int dircount = dir.Count();//獲得文件夾對象數量 int filecount = file.Count();//獲得文件對象數量 //循環文件夾 for (int i = 0; i < dircount; i++) { string pathNode = fullPath + "\\" + dir[i].Name; GetMultiFile(pathNode, isFullName); } //循環文件 for (int j = 0; j < filecount; j++) { if (isFullName) { fileList.Add(file[j].FullName); } else { fileList.Add(file[j].Name); } } return fileList.ToArray(); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n出錯的位置為:Form1.PaintTreeView()"); } return null; } private static bool GetMultiFile(string path, bool isFullName = false) { if (Directory.Exists(path) == false) { return false; } DirectoryInfo dirs = new DirectoryInfo(path); //獲得程序所在路徑的目錄對象 DirectoryInfo[] dir = dirs.GetDirectories();//獲得目錄下文件夾對象 FileInfo[] file = dirs.GetFiles();//獲得目錄下文件對象 int dircount = dir.Count();//獲得文件夾對象數量 int filecount = file.Count();//獲得文件對象數量 int sumcount = dircount + filecount; if (sumcount == 0) { return false; } //循環文件夾 for (int j = 0; j < dircount; j++) { string pathNodeB = path + "\\" + dir[j].Name; GetMultiFile(pathNodeB, isFullName); } //循環文件 for (int j = 0; j < filecount; j++) { if (isFullName) { fileList.Add(file[j].FullName); } else { fileList.Add(file[j].Name); } } return true; } #endregion