且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取将返回列表的文件夹和子文件夹中所有文件的方法

更新时间:2022-04-21 07:57:54

private List<String> DirSearch(string sDir)
{
    List<String> files = new List<String>();
    try
    {
        foreach (string f in Directory.GetFiles(sDir))
        {
            files.Add(f);
        }
        foreach (string d in Directory.GetDirectories(sDir))
        {
            files.AddRange(DirSearch(d));
        }
    }
    catch (System.Exception excpt)
    {
        MessageBox.Show(excpt.Message);
    }

    return files;
}

如果您不想在内存中加载整个列表并避免阻塞,您可以查看以下答案.

and if you don't want to load the entire list in memory and avoid blocking you may take a look at the following answer.