Unity 获取指定路径文件夹下所有文件集合(递归写法)(二)
方法代码的使用:
private List<string> fileList = new List<string>(); /// <summary> /// 获取路径下所有文件以及子文件夹中文件 /// </summary> /// <param name="path">全路径根目录</param> /// <param name="FileList">存放所有文件的全路径</param> /// <returns></returns> public static List<string> GetFile(string path, List<string> FileList) { DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] fil = dir.GetFiles(); DirectoryInfo[] dii = dir.GetDirectories(); foreach (FileInfo f in fil) { //int size = Convert.ToInt32(f.Length); long size = f.Length; FileList.Add(f.FullName);//添加文件路径到列表中 } //获取子文件夹内的文件列表,递归遍历 foreach (DirectoryInfo d in dii) { GetFile(d.FullName, FileList); } return FileList; } private void GetShowFile(string rootPath) { if (string.IsNullOrEmpty(rootPath)) return; var files = GetFile(rootPath, fileList); foreach (var item in files) { string info = new DirectoryInfo(item).Name; if (Path.GetExtension(info) == ".fbx") { string equipmentName = item; var tup = pool.Getprefab(); tup.Item2.cusName.text = Path.GetFileNameWithoutExtension(equipmentName); tup.Item2.pathname = equipmentName; tup.Item2.ui.OnBeginDragEvent.AddListener((Transform tr) => { Debug.Log("Begin"); isres = false; choose = equipmentName; }); tup.Item2.ui.OnEndDragEvent.AddListener((Transform tr) => { Debug.Log("End"); Vector3 pos = Input.mousePosition; if (GameApp.camera.ScreenPointToWorld(ref pos, 500)) { LoadObj(pos); Debug.Log("pos"); } else { MessageBox.Ins.ShowOk("提示", "无法放置在该位置", "好的"); } }); items.Add(tup.Item2); } } }
方法原型:
(一)
private List<string> fileList = new List<string>(); /// <summary> /// 获取路径下所有文件以及子文件夹中文件 /// </summary> /// <param name="path">全路径根目录</param> /// <param name="FileList">存放所有文件的全路径</param> /// <returns></returns> public static List<string> GetFile(string path, List<string> FileList) { DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] fil = dir.GetFiles(); DirectoryInfo[] dii = dir.GetDirectories(); foreach (FileInfo f in fil) { //int size = Convert.ToInt32(f.Length); long size = f.Length; FileList.Add(f.FullName);//添加文件路径到列表中 } //获取子文件夹内的文件列表,递归遍历 foreach (DirectoryInfo d in dii) { GetFile(d.FullName, FileList); } return FileList; }
(二)
public partial class FileGet { /// <summary> /// 私有变量 /// </summary> private static List<FileInfo> lst = new List<FileInfo>(); /// <summary> /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹) /// </summary> /// <param name="path">文件夹路径</param> /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param> /// <returns>List<FileInfo></returns> public static List<FileInfo> getFile(string path, string extName) { getdir(path, extName); return lst; } /// <summary> /// 私有方法,递归获取指定类型文件,包含子文件夹 /// </summary> /// <param name="path"></param> /// <param name="extName"></param> private static void getdir(string path, string extName) { try { string[] dir = Directory.GetDirectories(path); //文件夹列表 DirectoryInfo fdir = new DirectoryInfo(path); FileInfo[] file = fdir.GetFiles(); //FileInfo[] file = Directory.GetFiles(path); //文件列表 if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空 { foreach (FileInfo f in file) //显示当前目录所有文件 { if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) { lst.Add(f); } } foreach (string d in dir) { getdir(d, extName);//递归 } } } catch (Exception ex) { LogHelper.WriteLog(ex); throw ex; } } } public partial class FileGet1 { /// <summary> /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹) /// </summary> /// <param name="path">文件夹路径</param> /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param> /// <returns>List<FileInfo></returns> public static List<FileInfo> getFile(string path, string extName) { try { List<FileInfo> lst = new List<FileInfo>(); string[] dir = Directory.GetDirectories(path); //文件夹列表 DirectoryInfo fdir = new DirectoryInfo(path); FileInfo[] file = fdir.GetFiles(); //FileInfo[] file = Directory.GetFiles(path); //文件列表 if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空 { foreach (FileInfo f in file) //显示当前目录所有文件 { if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) { lst.Add(f); } } foreach (string d in dir) { getFile(d, extName);//递归 } } return lst; } catch (Exception ex) { LogHelper.WriteLog(ex); throw ex; } } }