/******************************************************************************
* SunnyUI 开源控件库、工具类库、扩展类库、多页面开发框架。
* CopyRight (C) 2012-2021 ShenYongHua(沈永华).
* QQ群:56829229 QQ:17612584 EMail:SunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 如果您使用此代码,请保留此说明。
******************************************************************************
* 文件名称: UDir.cs
* 文件说明: 目录扩展类
* 当前版本: V3.0
* 创建日期: 2020-01-01
*
* 2020-01-01: V2.2.0 增加文件说明
******************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Sunny.UI
{
///
/// 目录扩展类
///
public static class DirEx
{
///
/// 当前可执行文件路径,末尾包括文件夹分隔符(windows为\,linux为/)
///
/// 结果
public static string CurrentDir()
{
//return Environment.CurrentDirectory.DealPath();
return AppDomain.CurrentDomain.BaseDirectory.DealPath();
}
///
/// 选择文件夹
///
/// 说明
/// 返回true则path为选择文件夹路径
/// 显示新建文件夹按钮
/// 是否选择文件夹
public static bool SelectDir(string desc, ref string dir, bool showNewButton = true)
{
bool bOk = false;
using (FolderBrowserDialog fd = new FolderBrowserDialog { Description = desc, ShowNewFolderButton = showNewButton })
{
fd.SelectedPath = dir;
if (fd.ShowDialog() == DialogResult.OK)
{
dir = fd.SelectedPath.DealPath();
bOk = true;
}
}
return bOk;
}
///
/// 选择文件夹
///
/// 说明
/// 返回true则path为选择文件夹路径
/// 是否选择文件夹
public static bool SelectDirEx(string desc, ref string dir)
{
bool bOk = false;
using (FolderBrowserDialogEx fd = new FolderBrowserDialogEx { Description = desc, DirectoryPath = dir })
{
if (fd.ShowDialog(null) == DialogResult.OK)
{
dir = fd.DirectoryPath.DealPath();
bOk = true;
}
}
return bOk;
}
///
/// Temp文件夹,末尾包括\
///
/// 结果
public static string TempPath()
{
return Path.GetTempPath().DealPath();
}
///
/// Temp文件夹下唯一的新建临时文件夹,末尾包括\
///
/// 结果
public static string TempRandomPath()
{
string path = FileEx.TempFileName(false);
Directory.CreateDirectory(path);
return path.DealPath();
}
///
/// 检测指定目录是否存在
///
/// 目录的绝对路径
/// 结果
public static bool Exists(string dir)
{
return Directory.Exists(dir);
}
///
/// 创建一个目录
///
/// 目录的绝对路径
public static void CreateDir(string dir)
{
//如果目录不存在则创建该目录
if (!Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
///
/// 尝试删除非空文件夹
///
/// 文件夹
/// 结果
public static bool TryDelete(string dir)
{
try
{
Directory.Delete(dir);
return !Directory.Exists(dir);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
///
/// 清空指定的文件夹,但不删除文件夹
///
/// 文件夹
public static void EmptyDir(string dir)
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
{
FileInfo fi = new FileInfo(d);
if (fi.Attributes.ToString().IndexOf("ReadOnly", StringComparison.Ordinal) != -1)
{
fi.Attributes = FileAttributes.Normal;
}
fi.TryDelete();
}
else
{
DirectoryInfo d1 = new DirectoryInfo(d);
EmptyDir(d1.FullName); //递归删除子文件夹
TryDelete(d);
}
}
}
///
/// 删除指定的文件夹
///
/// 文件夹
public static void DeleteDir(string dir)
{
EmptyDir(dir);
TryDelete(dir);
}
///
/// 调用系统资源管理器打开文件夹,如果是文件则选中文件
///
/// 文件夹
public static void OpenDir(string dir)
{
if (File.Exists(dir))
{
System.Diagnostics.Process.Start("Explorer.exe", @"/select," + dir);
}
if (Exists(dir))
{
System.Diagnostics.Process.Start("Explorer.exe", dir);
}
}
///
/// 在指定目录下创建以年月日分级的子目录,末尾包括\
///
/// 日期
/// 文件夹
/// 不存在是否创建
/// 文件夹名
public static string YearMonthDayFolder(this DateTime dt, string path, bool createIfNotExist = false)
{
if (path.IsNullOrEmpty())
{
return path;
}
string result = path.DealPath() + dt.YearString() + "\\" + dt.YearMonthString() + "\\" + dt.DateString() + "\\";
if (createIfNotExist)
{
DirEx.CreateDir(result);
}
return result;
}
///
/// 在指定目录下创建以年月分级的子目录,末尾包括\
///
/// 日期
/// 文件夹
/// 不存在是否创建
/// 文件夹名
public static string YearMonthFolder(this DateTime dt, string path, bool createIfNotExist = false)
{
if (path.IsNullOrEmpty())
{
return path;
}
string result = path.DealPath() + dt.YearString() + "\\" + dt.YearMonthString() + "\\";
if (createIfNotExist)
{
DirEx.CreateDir(result);
}
return result;
}
///
/// 创建年月文件夹,末尾包括\
///
/// 路径
/// 时间
/// 文件夹
public static string CreateYearMonthFolder(string dir, DateTime datetime)
{
return datetime.YearMonthFolder(dir, true);
}
///
/// 创建年月日文件夹,末尾包括\
///
/// 路径
/// 时间
/// 文件夹
public static string CreateYearMonthDayFolder(string dir, DateTime datetime)
{
return datetime.YearMonthDayFolder(dir, true);
}
///
/// 获取指定目录中的匹配项(文件或目录)
///
/// 要搜索的目录
/// 项名模式(正则)。null表示忽略模式匹配,返回所有项
/// 递归深度。负数表示不限,0表示仅顶级
/// 是否抛异常
/// 结果
public static string[] GetFileSystemEntries(string dir, string regexPattern = null, int depth = 0, bool throwEx = false)
{
List lst = new List();
try
{
foreach (string item in Directory.GetFileSystemEntries(dir))
{
try
{
string filename = Path.GetFileName(item);
if (regexPattern == null || Regex.IsMatch(filename, regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))
{
lst.Add(item);
}
//递归
if (depth != 0 && (File.GetAttributes(item) & FileAttributes.Directory) == FileAttributes.Directory)
{
lst.AddRange(GetFileSystemEntries(item, regexPattern, depth - 1, throwEx));
}
}
catch
{
if (throwEx)
{
throw;
}
}
}
}
catch
{
if (throwEx)
{
throw;
}
}
return lst.ToArray();
}
///
/// 获取指定目录中的匹配文件
///
/// 要搜索的目录
/// 文件名模式(正则)。null表示忽略模式匹配,返回所有文件
/// 递归深度。负数表示不限,0表示仅顶级
/// 是否抛异常
/// 结果
public static string[] GetFiles(string dir, string regexPattern = null, int depth = 0, bool throwEx = false)
{
List lst = new List();
try
{
foreach (string item in Directory.GetFileSystemEntries(dir))
{
try
{
bool isFile = (File.GetAttributes(item) & FileAttributes.Directory) != FileAttributes.Directory;
string filename = Path.GetFileName(item);
if (isFile && (regexPattern == null || Regex.IsMatch(filename, regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)))
{
lst.Add(item);
}
//递归
if (depth != 0 && !isFile)
{
lst.AddRange(GetFiles(item, regexPattern, depth - 1, throwEx));
}
}
catch
{
if (throwEx)
{
throw;
}
}
}
}
catch
{
if (throwEx)
{
throw;
}
}
return lst.ToArray();
}
///
/// 获取指定目录中的匹配目录
///
/// 要搜索的目录
/// 目fu录名模式(正则)。null表示忽略模式匹配,返回所有目录
/// 递归深度。负数表示不限,0表示仅顶级
/// 是否抛异常
/// 结果
public static string[] GetDirectories(string dir, string regexPattern = null, int depth = 0, bool throwEx = false)
{
List lst = new List();
try
{
foreach (string item in Directory.GetDirectories(dir))
{
try
{
string filename = Path.GetFileName(item);
if (filename == null)
{
continue;
}
if (regexPattern == null || Regex.IsMatch(filename, regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))
{
lst.Add(item);
}
//递归
if (depth != 0)
{
lst.AddRange(GetDirectories(item, regexPattern, depth - 1, throwEx));
}
}
catch
{
if (throwEx)
{
throw;
}
}
}
}
catch
{
if (throwEx)
{
throw;
}
}
return lst.ToArray();
}
}
}