C# 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
发布日期:2021-05-10 09:24:57 浏览次数:18 分类:精选文章

本文共 5014 字,大约阅读时间需要 16 分钟。

            /**** Title���"���������" ������  */
/**** Theme������������������������������������������������ */
/**** Description��������������� */
/**** Date���2019��� */
/**** Version���0.1������ */
/**** Author���Coffee */
/**** Modifier��� */
            using System;
using System.Collections.Generic;
using System.IO;
namespace kernal
{
public class GetAllFileOfFolder
{
private static readonly List
_FileList = new List
();
private GetAllFileOfFolder()
{
// ���������������������������
_FileList = new List
();
}
#regionozillaModal
public static List
GetAllFile(string path, string extName)
{
if (string.IsNullOrWhiteSpace(path))
{
if (Directory.Exists(path))
{
GetAllFilesOfDirectory(path, extName);
}
else
{
Directory.CreateDirectory(path);
if (Directory.Exists(path))
{
GetAllFilesOfDirectory(path, extName);
}
}
}
else
{
// ���������������������������������
EverydayLog.Write("GetAllFileOfFolder/GetallFile()/������������������������������������������������������");
}
return _FileList;
}
#endregion
#region
private static void GetAllFilesOfDirectory(string path, string extName)
{
try
{
DirectoryInfo directory = new DirectoryInfo(path);
if (directory.Exists)
{
FileInfo[] files = directory.GetFiles();
if (files.Length > 0)
{
foreach (FileInfo file in files)
{
if (IsMatchExtension(extName, file.Extension))
{
_FileList.Add(file);
}
}
foreach (string subDirectory in directory.GetDirectories())
{
GetAllFilesOfDirectory(subDirectory, extName);
}
}
}
}
catch (Exception ex)
{
EverydayLog.Write("GetAllFileOfFolder/GetallfilesOfDir()/���������������������" + path +
" ������������������������������������������=" + ex.Message);
}
}
private static bool IsMatchExtension(string targetExt, string currentExt)
{
if (string.IsNullOrEmpty(targetExt) || string.IsNullOrEmpty(currentExt))
{
return false;
}
return string.Equals(currentExt, targetExt, StringComparison.OrdinalIgnoreCase);
}
}
}

������������������������

���������������������������������������������������������������������������������������������������������

���������������������������

1. GetAllFile(string path, string extName) ���������������������������������������

2. GetAllFilesOfDirectory(string path, string extName) ������������������������������������������������������

3. IsMatchExtension(string targetExt, string currentExt) ���������������������������������������������������

���������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:Unity实现数据变动实时在界面显示(通过委托的方式)
下一篇:Lua时间戳和日期转换

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月29日 21时37分32秒