using System;
using TopDAL;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string res = "";
ForeachFile(@"D:\123",ref res);
Console.Write(res);
Console.ReadKey();
}
/// <summary>
/// 遍历指定文件夹中的文件包括子文件夹的文件
/// </summary>
/// <param name="filePathByForeach">等待遍历的目录(绝对路径)</param>
/// <param name="result">遍历之后的结果</param>
/// <returns></returns>
public static void ForeachFile(string filePathByForeach,ref string result)
{
DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
FileInfo[] file= theFolder.GetFiles();//获取所在目录的文件
foreach (FileInfo fileItem in file) //遍历文件
{
result+="dirName:"+fileItem.DirectoryName+" fileName:"+fileItem.Name + "\n";
}
//遍历文件夹
foreach (DirectoryInfo NextFolder in dirInfo)
{
ForeachFile(NextFolder.FullName, ref result );
}
}
}
}
如果想点击某个控件调用该方法,
可以把方法写在代码页,在控件的事件中调用方法
原文链接:https://www.cnblogs.com/lin494910940/p/12396576.html