Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Recursive Directory Search in C#

What recls.NET Offers Above .NET's Search Facilities

You may be reading this and thinking "but there have been standard facilities for recursive filesystem search since CLR version 2". And you'd be right. DirectoryInfo's GetFiles() and GetDirectories() methods have a third overload that takes a parameter of type System.IO.SearchOption, which has the enumerators TopDirectoryOnly and AllDirectories. And it's the same for Directory's GetFiles() and GetDirectories() methods.

So what does recls 100% .NET provide that is not available in the standard libraries?

  • Multi-part patterns. Passing "*.dll|*.exe" to Directory.GetFiles() will throw an argument exception, and passing "*.dll;*.exe" will simply return no results.
  • Depth-control. With the CLR facilities you have only two choices: no recursion or infinite recursion.
  • IEntry - so you don't have to ask twice (DirectoryInfo.GetFiles()/GetDirectories() do return arrays of FileInfo/DirectoryInfo)
  • Filtering of hidden & system files. DirectoryInfo.GetFiles() always includes hidden and system files.
  • Ignoring inaccessible directories. An UnauthorizedAccessException is thrown by DirectoryInfo.GetFiles() when asking for all files for a directory that contains an inaccessible subdirectory (like the "System Volume Information" directory sitting in the root level on each local hard drive). Recls allows you to handle this, by specifying handler (via delegate or interface), or to skip any such items via specifying SearchOptions.IgnoreInaccessibleNodes.
  • Search-relative path. When I'm writing non-trivial filesystem manipulation programs -- such as recursive comparisons, recursive copying, and so on -- I find the SearchRelativePath invaluable. Writing such programs without it would be burdensome, to say the least.

The Future

As mentioned earlier, recls 100% .NET does not currently provide FTP searching. That's something that might be added in a later version, though at this stage it looks doubtful. Without a commercial imperative to do so it's likely to languish at the end of one of my long to-do lists.

Also, the new version does not support the specification of multiple patterns where one or more includes a sub-directory, as in:

FileSearcher.Search(@"C:\Windows", "system/*.dll|system32/*.dll");

This will be added in a future version.

I am considering a future facility to treat the patterns parameter as a regular expression, which would probably be indicated by a new SearchOptions flag. (The main reason I haven't yet is I'm still in two minds about whether (and how) to handle multiple patterns in that form. I'm definitely interested in opinions from users/readers on the subject.)

Finally, other languages will be getting the recls 100% treatment, probably starting with Ruby or Python next year.

Obtaining recls 100% .NET

recls 100% .NET is available, from http://recls.net. The download includes the library (which incorporates all the core functionality discussed in this article) along with documentation (Intellisense XML and CHM), and example projects.


I'd like to thank my .NET posse -- Chris Oldwood, Garth Lancaster, John O'Halloran and Joy Chan -- for their assistance in keeping me to the point and making it interesting. Any failures are my own fault for inadequately addressing their concerns.


[1] The recls project; http://recls.org/

[2] http://en.wikipedia.org/wiki/FxCop

[3] Introducing recls, Matthew Wilson, C/C++ Users Journal, November 2003

[4] Extended STL, volume 1: Collections and Iterators, Matthew Wilson, Addison-Wesley, 2007

[5] Quality Matters, Part 1: Introductions, and Nomenclature, Matthew Wilson, Overload 92, August 2009

[6] Code Complete, 2nd Edition, Steve McConnell, Microsoft Press, 2004

[7] An Enhanced ostream_iterator, Matthew Wilson, Dr. Dobb's Journal, June 2007

[8] C# In Depth, Jon Skeet, Manning, 2008

[9] More Effective C#, Bill Wagner, Addison-Wesley, 2009

Matthew Wilson is a software development consultant and trainer for Synesis Software who helps clients to build high-performance software that does not break, specializing in C++ and C#/.NET. He is the author of the books Imperfect C++ and Extended STL, a columnist for ACCU, and a resident guru at Dr. Dobb's CodeTalk, focusing on Windows technologies. He can be contacted at [email protected]

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.