How do you count every subfolder under the Outlook mailbox?
<p>Using the Microsoft Outlook 14.0 Object Library reference and the code below I would like to count all folders listed under the mailbox including every subfolder but I have a problem.  </p>

<p><img src="http://i.stack.imgur.com/PQDZL.jpg" alt="enter image description here"></p>

<p>This code only counts the top level folders and second level folder but doesn't count any subfolders beneath. Something wrong within my foreach statement in the countRootFolders method but I can't work it out. Can anyone help? </p>

<pre><code>Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;

private void button1_Click(object sender, EventArgs e)
{
    app = new Microsoft.Office.Interop.Outlook.Application();
    ns = app.GetNamespace("MAPI");
    MessageBox.Show(countRootFolders().ToString());
}

public int countRootFolders()
{
    Microsoft.Office.Interop.Outlook.MAPIFolder rootFolder = this.ns.Session.DefaultStore.GetRootFolder();
    int rootCount = rootFolder.Folders.Count;

    foreach (Microsoft.Office.Interop.Outlook.MAPIFolder subfolder in rootFolder.Folders)
    {
        rootCount += subfolder.Folders.Count;
    }

    return rootCount;
}
</code></pre>

<p>Any help greatly appreicated!!</p>
