What are all the ways to traverse directory trees?

后端 未结 7 1112
挽巷
挽巷 2021-01-02 04:13

How do you traverse a directory tree in your favorite language?

What do you need to know to traverse a directory tree in different operating systems? On different f

7条回答
  •  被撕碎了的回忆
    2021-01-02 04:40

    In C#:

    Stack dirs = new Stack();
    
    dirs.Push(new DirectoryInfo("C:\\"));
    
    while (dirs.Count > 0) {
        DirectoryInfo current = dirs.Pop();
    
        // Do something with 'current' (if you want)
    
        Array.ForEach(current.GetFiles(), delegate(FileInfo f)
        {
            // Do something with 'f'
        });
    
        Array.ForEach(current.GetDirectories(), delegate(DirectoryInfo d)
        {
            dirs.Push(d);
        });
    }
    

提交回复
热议问题