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 ▼

Matthew Wilson

Dr. Dobb's Bloggers

Recursive search examples, pt1: C#

November 20, 2009

In light of my recent article on recls 100% .NET, I thought I'd do some bits on the recls library: the C/C++ version, and the new .NET version.

First up, let's write a utility that, say, reports the number of C# source files in/under the current directory, excluding the ubiquitous Properties/AssemblyInfo.cs, and reports on the total size of all. We'll write it in C# using recls 100%.NET and in C & C++ using recls. As you'll see, we see a pretty obvious difference in development effort, which probably isn't a surprise. What is a surprise, given the application domain, is there's a substantial performance difference, which I think might be due to the .NET runtime.

Here's the first version of the code:

<br />namespace CSharpSourceFinder  <br />{  <br />  using Recls;  <br />  <br />  using System;  <br />  <br />  class Program  <br />  {  <br />    static void Main(string[] args)  <br />    {  <br />      int numFiles = 0;  <br />      long totalSize = 0;  <br />  <br />      foreach(IEntry entry in FileSearcher.Search(null, "*.cs"))  <br />      {  <br />        if(0 == String.Compare("AssemblyInfo.cs", entry.File, StringComparison.CurrentCultureIgnoreCase))  <br />        {  <br />          if(0 == String.Compare(@"Properties\f", entry.DirectoryParts[entry.DirectoryParts.Count - 1], StringComparison.CurrentCultureIgnoreCase))  <br />          {  <br />            continue;  <br />          }  <br />        }  <br />  <br />        ++numFiles;  <br />        totalSize += entry.Size;  <br />      }  <br />  <br />      Console.Out.WriteLine("{0} file(s); {1} bytes", numFiles, totalSize);  <br />    }  <br />  }  <br />} 

This looks good. Unfortunately, when I ran it on in the root directory of my work drive, I (eventually) ended up with a System.UnauthorizedAccessException, thrown when an inaccessible (but non-hidden, non-system) directory was encountered. Now, the directory was created several months ago to test this very behaviour in recls, but that doesn't mean it should not be handled appropriately. To handle this, we need to make a change:

 <br />. . .  <br />      foreach(IEntry entry in FileSearcher.Search(null, "*.cs"<strong>, SearchOptions.IgnoreInaccessibleNodes</strong>))  <br />. . . 

This now succeeds, and the output (run via a utility that measures process time characteristics) is:

    1354 file(s); 6540661 bytes
    time: elapsed: 20593ms, kernel: 11796ms, user: 8640ms

This took me less than 10 minutes to write. In the next post, I'll look at the C equivalent.

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.