当前位置:网站首页>C # "learning code snippet" - recursively obtain all files under the folder
C # "learning code snippet" - recursively obtain all files under the folder
2022-07-25 00:29:00 【solar storm】
C# Recursively get all the files under the folder
introduction : Record C# Recursively get under folder All the files code snippet
One 、 code snippet
/// <summary>
/// Get the final required folder path string collection
/// </summary>
/// <param name="folderName"></param>
/// <returns></returns>
private List<string> getFiles(string folderName, bool useOnlyName = false)
{
List<string> fileNameList = new List<string>() {
};
DirectoryInfo theFolder = new DirectoryInfo(folderName);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
fileNameList = getFileList(dirInfo, fileNameList, useOnlyName);
FileInfo[] fileInfo = theFolder.GetFiles();
for (int i = 0; i < fileInfo.Length; i++)
{
if(useOnlyName)
{
fileNameList.Add(fileInfo[i].Name);
}
else
{
fileNameList.Add(fileInfo[i].FullName);
}
}
return fileNameList;
}
/// <summary>
/// Recursively get all files in the folder ( Include files in subfolders )
/// </summary>
/// <param name="dirInfos"></param>
/// <param name="fileNameList"></param>
/// <returns></returns>
private List<string> getFileList(DirectoryInfo[] dirInfos, List<string> fileNameList, bool useOnlyName = false)
{
foreach (DirectoryInfo sonFolder in dirInfos)
{
FileInfo[] fileInfo = sonFolder.GetFiles();
for (int i = 0; i < fileInfo.Length; i++)
{
if(useOnlyName)
{
fileNameList.Add(fileInfo[i].Name);
}
else
{
fileNameList.Add(fileInfo[i].FullName);
}
}
if (sonFolder.GetDirectories() != null)
{
getFileList(sonFolder.GetDirectories(), fileNameList, useOnlyName);
}
}
return fileNameList;
}
}
Two 、 Use cases
// Get the full path of the file
List<string> fileWithPath = getFiles("./Documents/Help/Pages");
// Just get the file name
List<string> fileWithPath = getFiles("./Documents/Help/Pages", true);
3、 ... and 、 test result
Get the full path :
Get the file name 
边栏推荐
- ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
- Multi merchant mall system function disassembly Lecture 14 - platform side member level
- Basic functions of tea
- [LeetCode周赛复盘] 第 83 场双周赛20220723
- Promtool Check
- Netease game Flink SQL platform practice
- Install and configure php5-7 version under centos7.4
- [mindspore] [mode] spontaneous_ The difference between mode and graph mode
- UART
- 2022 Henan Mengxin League game (2): Henan University of technology d-pair
猜你喜欢

Dpdk based basic knowledge sorting-01

Quartus: install cyclone 10 LP device library for quartus version 17.1

Which automation tools can double the operation efficiency of e-commerce?

每周小结(*66):下一个五年

Only by learning these JMeter plug-ins can we design complex performance test scenarios

Two numbers that appear only once in the array

Detailed usage of iperf

UART

C语言学习之分支与循环语句

Wechat applet development learning 5 (custom components)
随机推荐
BGP机房和BGP
Kubernetes application design guide
mysql初次安装的root密码是什么
First experience of flask
Soft test --- fundamentals of programming language (Part 2)
EF core :自引用的组织结构树
Leetcode 1260. two dimensional grid migration: two solutions (k simulations / one step)
codeforces round #805 ABCDEFG
paddlepaddle论文系列之Alexnet详解(附源码)
===、==、Object. Is basic package type
2022 Henan Mengxin League game (2): Henan University of technology d-pair
Dpdk based basic knowledge sorting-01
QT learning - using database singleton to complete login matching + registration function
LeetCode_6124_第一个出现两次的字母
@Mapkey usage instructions
[acwing weekly rematch] 61st weekly 20220723
UART
R language uses ISNA function to check whether the list and dataframe contain missing values, marks abnormal values in data columns in dataframe as missing values Na, and uses na.omit function to dele
Advanced function of postman
Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库