How can I optimize this method?
<p>I would like to optimize the following method, that returns the total file count of the specified folder and all subfolders, for speed and memory usage; any advice is appreciated.  </p>

<p>Thanks.</p>

<pre><code>private int countfiles(string srcdir)
{
    try
    {
        DirectoryInfo dir = new DirectoryInfo(srcdir);

        //if the source dir doesn't exist, throw an exception
        if (!dir.Exists)
            throw new ArgumentException("source dir doesn't exist -&gt; " + srcdir);

        int count = dir.GetFiles().Length;

        //loop through each sub directory in the current dir
        foreach (DirectoryInfo subdir in dir.GetDirectories())
        {
            //recursively call this function over and over again
            count += countfiles(subdir.FullName);
        }

        //cleanup
        dir = null;

        return count;
    }
    catch (Exception exc)
    {
        MessageBox.Show(exc.Message);
        return 0;
    }           
}
</code></pre>

<h3>So I did some benchmarking with the suggestions that were proposed.  Here are my findings:</h3>

<ul>
<li><p>My method, with recursion, is the slowest finding 9062 files in a directory tree in 6.234 seconds.</p></li>
<li><p>@Matthew’s answer, using SearchOption.AllDirectories,  is the fastest finding the same 9062 files in 4.546 seconds </p></li>
<li><p>@Jeffery’s answer, using LINQ, is in the middle of the pack finding the same 9062 files in 5.562 seconds.</p></li>
</ul>

<p>Thank you everyone for your suggestions.</p>
