C# 递归遍历所有目录,文件

C# 递归遍历所有目录,文件

// 调用 GetAllDirList("D:\\001");
static List<string> folderList = new List<string>();
/// <summary>
/// 递归遍历所有目录
/// </summary>
/// <param name="dir">传入路径</param>
static void GetAllDirList(string dir)
{
    DirectoryInfo di = new DirectoryInfo(dir);
    foreach (DirectoryInfo diNext in di.GetDirectories())
    {
        // 添加目录
        folderList.Add(diNext.FullName);
        GetAllDirList(diNext.FullName);
    }
}


// 调用 GetAllFileList("D:\\001");
static List<string> fileList = new List<string>();
/// <summary>
/// 递归遍历所有文件
/// </summary>
/// <param name="dir">传入路径</param>
static void GetAllFileList(string dir)
{
    DirectoryInfo di = new DirectoryInfo(dir);
    foreach (DirectoryInfo diNext in di.GetDirectories())
    {
        // 遍历目录下的文件
        foreach (FileInfo NextFile in diNext.GetFiles())
        {
            // 标记特殊文件
            //if (NextFile.Name.Equals("data.mdb"))
            //{
            // 添加文件
            fileList.Add(diNext.FullName + "\\" + NextFile.Name);
            //}
        }
        GetAllFileList(diNext.FullName);
    }
}


热门推荐