Actually, I think my translation of Microsoft-ese may have been
incorrect. Reparse points (like mapped drives) that can create a
circular or cyclical reference, is the problem, not shortcuts.
http://msdn.microsoft.com/en-us/library/ms143448.aspx (See the remarks)
The following link also has some useful information on traversing
the file system, and other potential pitfalls like directory access
rights, and so on:
http://msdn.microsoft.com/en-us/library/bb513869.aspxAs far as 'stack-o-phobia' goes
, the depth of recursion equals
the depth of folder nesting and from my calculations, unless there
are folders nested ~250 levels deep or more, I think recursion is
a safe approach. The use of the stack can also be minimized by
using non-static methods and storing the current folder in a non
static member variable, rather than passing it as an argument to
the recursive method (which is stored in each call's stack frame).
CLR 4.0 has tail-recursion optimization, but only for F#, by way of
that compiler. From what I've read, the problem with tail-recursion
optimization in .NET is that the security model relies heavily on call
stack introspection to detect the calling context of methods (e.g.,
examine the call stack to see who is calling your method), which
can preclude optimizing tail-recursive calls into iterative loops.
One reason that purely-recursive or functional approaches with no
side-effects are desirable for solving problems like this, is that they
are better suited for parallel execution (even though in this case,
disk I/O is the likely bottleneck and limiting factor).
A stack-based approach could not easily run on mutliple CPU cores.
SearchOption.AllDirectories
From the docs: ''...follows symbolic links...', which is
microsoft-ese for 'follows shortcuts'.
IOW, when it finds a shortcut to a folder that was
already processed in a child of that folder, it will
process the parent folder again.
IOW, this option is dangerous because it can lead
to infinite recursion.
First Time I've read that Tony ... and the SearchOption.AllDirectories has been around for a while ... surprised it's not plastered all over the web.