/// <summary> /// 获取指定文件夹中所有子文件夹列表 /// </summary> /// <param name="dirPath">目录路径(绝对路径)</param> /// <param name="searchPattern"> /// 搜索模式('*' 代码0或N个字符,'?' 代表1个字符) /// <para>列如:"Log*.log" 表示搜索所有Log开头的log文件</para> /// </param> /// <param name="SearchOption">是否搜索子目录</param> /// <returns>文件夹列表</returns> public static List <FileAttrModel> GetDirSubDirs(string dirPath, string searchPattern = "*", bool searchOption = false) { if (!IsExistDir(dirPath)) { throw new FileNotFoundException(); } List <FileAttrModel> list = new List <FileAttrModel>(); DirectoryInfo folder = new DirectoryInfo(dirPath); DirectoryInfo[] dirs; if (string.IsNullOrEmpty(searchPattern)) { searchPattern = "*"; } if (searchOption) { dirs = folder.GetDirectories(searchPattern, SearchOption.AllDirectories); } else { dirs = folder.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly); } try { foreach (DirectoryInfo dir in dirs) { FileAttrModel model = new FileAttrModel(); model.FileNmae = dir.Name; model.FileFullName = dir.FullName; model.Extension = dir.Extension; model.CreateTime = dir.CreationTime; model.LastAccessTime = dir.LastAccessTime; model.LastWriteTime = dir.LastWriteTime; model.IsReadOnly = (dir.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; model.IsHidden = (dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; list.Add(model); } return(list); } catch (IOException ex) { throw ex; } }
/// <summary> /// 获取指定文件夹中的文件列表 /// </summary> /// <param name="dirPath">目录路径(绝对路径)</param> /// <param name="searchPattern"> /// 搜索模式('*' 代码0或N个字符,'?' 代表1个字符) /// <para>列如:"Log*.log" 表示搜索所有Log开头的log文件</para> /// </param> /// <param name="SearchOption">是否搜索子目录</param> /// <returns>文件列表</returns> public static List <FileAttrModel> GetDirFilesAttr(string dirPath, string searchPattern = "*", bool searchOption = false) { if (!IsExistDir(dirPath)) { throw new FileNotFoundException(); } List <FileAttrModel> list = new List <FileAttrModel>(); DirectoryInfo folder = new DirectoryInfo(dirPath); FileInfo[] files; if (string.IsNullOrEmpty(searchPattern)) { searchPattern = "*"; } if (searchOption) { files = folder.GetFiles(searchPattern, SearchOption.AllDirectories); } else { files = folder.GetFiles(searchPattern, SearchOption.TopDirectoryOnly); } foreach (FileInfo file in files) { FileAttrModel model = new FileAttrModel(); model.FileNmae = file.Name; model.FileFullName = file.FullName; model.Extension = file.Extension; model.FileSize = file.Length; model.CreateTime = file.CreationTime; model.LastAccessTime = file.LastAccessTime; model.LastWriteTime = file.LastWriteTime; model.IsReadOnly = file.IsReadOnly; model.IsHidden = (file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; //model.ParentPath = file.DirectoryName; list.Add(model); } return(list); }