/// <summary> /// 获得指定目录下所有文件或文件夹的相对路径 /// </summary> /// <param name="path"></param> /// <param name="flags">标记位</param> /// <returns></returns> internal static IEnumerable <string> GetDirectoryContainsByRelativePathInternal(string prefix, string path, GetPathOptions flags = GetPathOptions.Default) { if (path.IsNullOrEmpty()) { throw new ArgumentException($"{nameof(path)} can not be null of empty."); } if (!Directory.Exists(path)) { throw new DirectoryNotFoundException($"The directory {path} was not found."); } if ((flags & GetPathOptions.IncludeDirectory) != 0 && !prefix.IsNullOrEmpty()) { yield return(prefix + Path.DirectorySeparatorChar); } if ((flags & GetPathOptions.IncludeFile) != 0) { string[] files = null; try { files = Directory.GetFiles(path); } catch (Exception) { } if (files != null) { foreach (var file in files) { yield return(Path.Combine(prefix, Path.GetFileName(file))); } } } if ((flags & GetPathOptions.ScanSubDirectory) != 0) { string[] subdirs = null; try { subdirs = Directory.GetDirectories(path); } catch (Exception) { } if (subdirs != null) { foreach (var subdir in subdirs) { foreach (var file in GetDirectoryContainsByRelativePathInternal(Path.Combine(prefix, Path.GetFileName(subdir)), subdir, flags)) { yield return(file); } } } } }
/// <summary> /// 获得指定目录下所有文件或文件夹的相对路径 /// </summary> /// <param name="path"></param> /// <param name="flags">标记位</param> /// <returns></returns> internal static IEnumerable<string> GetDirectoryContainsByRelativePathInternal(string prefix, string path, GetPathOptions flags = GetPathOptions.Default) { if (path.IsNullOrEmpty()) throw new ArgumentException($"{nameof(path)} can not be null of empty."); if (!Directory.Exists(path)) throw new DirectoryNotFoundException($"The directory {path} was not found."); if ((flags & GetPathOptions.IncludeDirectory) != 0 && !prefix.IsNullOrEmpty()) { yield return prefix + Path.DirectorySeparatorChar; } if ((flags & GetPathOptions.IncludeFile) != 0) { string[] files = null; try { files = Directory.GetFiles(path); } catch (Exception) { } if (files != null) { foreach (var file in files) { yield return Path.Combine(prefix, Path.GetFileName(file)); } } } if ((flags & GetPathOptions.ScanSubDirectory) != 0) { string[] subdirs = null; try { subdirs = Directory.GetDirectories(path); } catch (Exception) { } if (subdirs != null) { foreach (var subdir in subdirs) { foreach (var file in GetDirectoryContainsByRelativePathInternal(Path.Combine(prefix, Path.GetFileName(subdir)), subdir, flags)) { yield return file; } } } } }
/// <summary> /// 获得指定目录下所有文件或文件夹的相对路径 /// </summary> /// <param name="path"></param> /// <param name="flags">标记位</param> /// <returns></returns> public static IEnumerable <string> GetDirectoryContainsByRelativePath(string path, GetPathOptions flags = GetPathOptions.Default) => GetDirectoryContainsByRelativePathInternal(string.Empty, path, flags);
/// <summary> /// 获得指定目录下所有文件或文件夹的相对路径 /// </summary> /// <param name="path"></param> /// <param name="flags">标记位</param> /// <returns></returns> public static IEnumerable<string> GetDirectoryContainsByRelativePath(string path, GetPathOptions flags = GetPathOptions.Default) => GetDirectoryContainsByRelativePathInternal(string.Empty, path, flags);