Directory.EnumerateDirectories/EnumerateFileSystemEntries/EnumerateFiles
Directory vs DirectoryInfo
Directory為static, 比較省資源, 只有查找檔名時使用
DirectoryInfo為建立File或Directory時使用
EnumerateFileSystemEntries vs GetFileSystemEntries
The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries, you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries, you must wait for the whole array of entries to be returned before…Therefore, when you are working with many files and directories, EnumerateFileSystemEntries can be more efficient.
//測試資料夾檔案結構
D:\Doc\UnitTest
- 2022
- 20220506
22-1.txt
- 2023
1.txt
//測試結果
=========Directory.EnumerateDirectories=======
D:\Doc\UnitTest\2022
D:\Doc\UnitTest\2023
=========Directory.EnumerateFileSystemEntries=========
D:\Doc\UnitTest\1.txt
D:\Doc\UnitTest\2022
D:\Doc\UnitTest\2023
=========Directory.EnumerateFiles=========
D:\Doc\UnitTest\1.txt
=========DirectoryInfo.GetFileSystemInfos=======
1.txt
2022
2023
=========DirectoryInfo.GetDirectories=======
2022
2023
取得特定層數的資料夾或檔名目錄
/// <summary>
/// 取得第幾層的資料夾路徑或檔案List
/// </summary>
/// <remarks>layers limited</remarks>
/// <param name="path">file root</param>
/// <param name="layer">how many layers</param>
/// <param name="returnType">files or dirs or all</param>
/// <returns></returns>
public IEnumerable<string> EnumerateSubDirectoriesOrFiles(
string path
, int layer
, string returnType)
{
var result = new List<string>();
if (!Directory.Exists(path))
{
return result;
}
layer--;
foreach (var subPath
in Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly))
{
if (layer == 0 && returnType=="files")
{
result.AddRange(Directory.EnumerateFiles(subPath, "*").ToList());
break;
}
if (layer == 0 && returnType == "all")
{
result.AddRange(Directory.EnumerateFiles(subPath, "*").ToList());
}
result.AddRange(layer == 0
? new List<string> { subPath }
: EnumerateSubDirectoriesOrFiles(subPath, layer, returnType));
}
return result;
}
//測試結果
=========EnumerateSubDirectoriesOrFiles 1-files =======
D:\Doc\UnitTest\2022\22-1.txt
=========EnumerateSubDirectoriesOrFiles 2-files =======
D:\Doc\UnitTest\2022\20220506\220506-1.txt
=========EnumerateSubDirectoriesOrFiles 3-files =======
=========EnumerateSubDirectoriesOrFiles 1-dirs =======
D:\Doc\UnitTest\2022
D:\Doc\UnitTest\2023
=========EnumerateSubDirectoriesOrFiles 2-dirs =======
D:\Doc\UnitTest\2022\20220506
=========EnumerateSubDirectoriesOrFiles 3-dirs =======
=========EnumerateSubDirectoriesOrFiles 1-dirs and files =======
D:\Doc\UnitTest\2022\22-1.txt
D:\Doc\UnitTest\2022
D:\Doc\UnitTest\2023
=========EnumerateSubDirectoriesOrFiles 2-dirs and files =======
D:\Doc\UnitTest\2022\20220506\220506-1.txt
D:\Doc\UnitTest\2022\20220506
=========EnumerateSubDirectoriesOrFiles 3-dirs and files =======
[Test]
public void EnumerateDirAndFile_OK()
{
string testPath = directoryTestBasePath;
var directories = Directory.EnumerateDirectories(testPath, "*");
Console.WriteLine("=========Directory.EnumerateDirectories=======");
PrintEnumString(directories);
var fileSystemEntries = Directory.EnumerateFileSystemEntries(testPath, "*");
Console.WriteLine("=========Directory.EnumerateFileSystemEntries=========");
PrintEnumString(fileSystemEntries);
var files = Directory.EnumerateFiles(testPath, "*");
Console.WriteLine("=========Directory.EnumerateFiles=========");
PrintEnumString(files);
var directoriesInfo = new DirectoryInfo(directoryTestBasePath);
Console.WriteLine("=========DirectoryInfo.GetFileSystemInfos=======");
foreach (var tmp in directoriesInfo.GetFileSystemInfos())
{
Console.WriteLine(tmp.Name);
}
Console.WriteLine("=========DirectoryInfo.GetDirectories=======");
foreach (var tmp1 in directoriesInfo.GetDirectories())
{
Console.WriteLine(tmp1.Name);
}
}
[Test]
public void EnumerateSubDirectories_OK()
{
var result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 1, "files");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 1-files =======");
PrintEnumString(result);
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 2, "files");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 2-files =======");
PrintEnumString(result);
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 3, "files");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 3-files =======");
PrintEnumString(result);
Console.WriteLine("");
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 1, "dirs");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 1-dirs =======");
PrintEnumString(result);
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 2, "dirs");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 2-dirs =======");
PrintEnumString(result);
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 3, "dirs");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 3-dirs =======");
PrintEnumString(result);
Console.WriteLine("");
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 1, "all");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 1-dirs and files =======");
PrintEnumString(result);
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 2, "all");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 2-dirs and files =======");
PrintEnumString(result);
result = EnumerateSubDirectoriesOrFiles(directoryTestBasePath, 3, "all");
Console.WriteLine("=========EnumerateSubDirectoriesOrFiles 3-dirs and files =======");
PrintEnumString(result);
}
//Create測試資料夾及檔案
string directoryTestBasePath = @"D:/Doc/UnitTest";
[OneTimeSetUp]
public void OneTimeInit()
{
if (!File.Exists(directoryTestBasePath))
{
DirectoryInfo directoryTestBaseDirInfo = new DirectoryInfo(directoryTestBasePath);
directoryTestBaseDirInfo.Create();
DirectoryInfo dir1 = new DirectoryInfo(Path.Combine(directoryTestBasePath, "2022"));
dir1.Create();
FileInfo file1 = new FileInfo(Path.Combine(dir1.FullName, "22-1.txt"));
file1.Create();
dir1 = new DirectoryInfo(Path.Combine(dir1.FullName, "20220506"));
dir1.Create();
file1 = new FileInfo(Path.Combine(dir1.FullName, "220506-1.txt"));
file1.Create();
dir1 = new DirectoryInfo(Path.Combine(directoryTestBasePath, "2023"));
dir1.Create();
file1 = new FileInfo(Path.Combine(directoryTestBaseDirInfo.FullName, "1.txt"));
file1.Create();
}
}