|
|
@@ -8,6 +8,12 @@ namespace Y.Utils.FileUtils
|
|
|
{
|
|
|
public class FileTool
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// 获取文件(单层目录)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path">路径</param>
|
|
|
+ /// <param name="pattern">通配符</param>
|
|
|
+ /// <returns></returns>
|
|
|
public static List<string> GetFile(string path, string pattern = "*")
|
|
|
{
|
|
|
if (Directory.Exists(path))
|
|
|
@@ -19,6 +25,12 @@ namespace Y.Utils.FileUtils
|
|
|
catch (Exception e) { }
|
|
|
return null;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 获取文件(所有目录)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path">路径</param>
|
|
|
+ /// <param name="pattern">通配符</param>
|
|
|
+ /// <returns></returns>
|
|
|
public static List<string> GetAllFile(string path, string pattern = "*")
|
|
|
{
|
|
|
List<string> result = null;
|
|
|
@@ -29,6 +41,12 @@ namespace Y.Utils.FileUtils
|
|
|
catch (Exception e) { }
|
|
|
return result;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 获取文件(所有目录)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path">路径</param>
|
|
|
+ /// <param name="pattern">通配符(支持多个通配符)</param>
|
|
|
+ /// <returns></returns>
|
|
|
public static List<string> GetAllFile(string path, string[] pattern)
|
|
|
{
|
|
|
List<string> result = new List<string>();
|
|
|
@@ -42,12 +60,18 @@ namespace Y.Utils.FileUtils
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 获取文件(所有目录)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="paths">路径(支持多个路径)</param>
|
|
|
+ /// <param name="patterns">通配符(支持多个通配符)</param>
|
|
|
+ /// <returns></returns>
|
|
|
public static List<string> GetAllFile(string[] paths, string[] patterns)
|
|
|
{
|
|
|
List<string> result = new List<string>();
|
|
|
if (!ListTool.IsNullOrEmpty(paths))
|
|
|
{
|
|
|
- foreach(var path in paths)
|
|
|
+ foreach (var path in paths)
|
|
|
{
|
|
|
if (!ListTool.IsNullOrEmpty(patterns))
|
|
|
{
|
|
|
@@ -56,7 +80,8 @@ namespace Y.Utils.FileUtils
|
|
|
List<string> temp = GetAllFile(path, pattern);
|
|
|
if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
|
|
|
}
|
|
|
- }else
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
List<string> temp = GetAllFile(path);
|
|
|
if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
|
|
|
@@ -65,6 +90,42 @@ namespace Y.Utils.FileUtils
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 获取文件(所有目录)(严格模式:从第一个.开始截取后缀)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="paths">路径(支持多个路径)</param>
|
|
|
+ /// <param name="patterns">通配符(支持多个通配符)</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<string> GetAllFileByExt(string[] paths, string[] patterns)
|
|
|
+ {
|
|
|
+ List<string> result = new List<string>();
|
|
|
+ if (!ListTool.IsNullOrEmpty(paths))
|
|
|
+ {
|
|
|
+ foreach (var path in paths)
|
|
|
+ {
|
|
|
+ List<string> temp = GetAllFile(path);
|
|
|
+ if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!ListTool.IsNullOrEmpty(patterns) && !ListTool.IsNullOrEmpty(result))
|
|
|
+ {
|
|
|
+ for (int i = result.Count() - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ string ext = Path.GetFileName(result[i]);
|
|
|
+ if (ext.IndexOf('.') >= 0)
|
|
|
+ {
|
|
|
+ ext = ext.Substring(ext.IndexOf('.'));
|
|
|
+ }
|
|
|
+ if (!patterns.Contains(ext)) result.RemoveAt(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 删除文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="file">文件路径</param>
|
|
|
+ /// <returns></returns>
|
|
|
public static bool Delete(string file)
|
|
|
{
|
|
|
try
|
|
|
@@ -82,6 +143,11 @@ namespace Y.Utils.FileUtils
|
|
|
catch { }
|
|
|
return false;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 删除文件(多个)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="files">文件路径(支持多个文件路径)</param>
|
|
|
+ /// <returns></returns>
|
|
|
public static bool Delete(string[] files)
|
|
|
{
|
|
|
bool result = true;
|