Recursively walking through a directory tree and listing file names

后端 未结 2 866
孤城傲影
孤城傲影 2021-01-02 10:03

I\'m trying to walk through a whole directory tree and print out all the file names on a listbox control. I wrote some code but there are errors. Not sure what I\'m doing wr

2条回答
  •  被撕碎了的回忆
    2021-01-02 10:37

    Use GetDirectories() overload accepting SearchOption:

    string[] dirs = Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
    foreach(dir)
    {
        ...
    }
    

    or better EnumerateFiles():

    IEnumerable files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories))
    foreach(files)
    {
        ...
    }
    

    Notice it performs lazy filesystem scan.

提交回复
热议问题