/// <summary>
/// 获取所有文件信息
/// </summary>
/// <param name="path">文件夹路径,null则为程序根目录</param>
/// <param name="exeName">文件后缀名数组{ ".mp4", ".avi", ".mov" },为null,或者数组包含*.*,则为获取全部文件</param>
/// <param name="subDir">true搜索子文件夹,false只搜索当前文件夹</param>
/// <returns>返回获取到的文件信息列表</returns>
public static List<FileInfo> GetAllFiles(string path, string[] exeName, bool subDir)
{
List<FileInfo> fileInfos = new List<FileInfo>();
try
{
if (exeName.Length > 0)
{
for (int i = 0; i < exeName.Length; i++)
{
exeName[i] = exeName[i].ToLower();
}
}
if (string.IsNullOrEmpty(path)) path = @".\";
string[] dir = Directory.GetDirectories(path);
DirectoryInfo fdir = new DirectoryInfo(path);
IEnumerable<FileInfo> fileInfo;
if (exeName == null || exeName.Contains("*.*"))
{
fileInfo = fdir.GetFiles();
}
else
{
fileInfo = fdir.GetFiles().Where(f => exeName.Contains(Path.GetExtension(f.FullName).ToLower()));
}
fileInfos.AddRange(fileInfo);
if (subDir)
{
foreach (string d in dir)
{
GetAllFiles(d, exeName, subDir);//递归
}
}
}
catch (Exception)
{
}
return fileInfos;
}
c# 获取指定后缀的文件名
于 2024-07-27 17:41:19 首次发布