使用Microsoft Outlook 14.0对象库参考和下面的代码我想计算邮箱下列出的所有文件夹,包括每个子文件夹,但我遇到了问题。如何计算Outlook邮箱下的每个子文件夹?
此代码只计算最高级别文件夹和第二级文件夹,但不能指望下所有子文件夹。在countRootFolders方法的foreach语句中出现错误,但我无法解决它。谁能帮忙?
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;
}
任何帮助大大appreicated !!
为了理解递归,首先必须理解递归。 – zeroef 2011-12-21 19:09:46
新代码... MessageBox.Show(countRootFolders(rootFolder).ToString()); public int countRootFolders(Microsoft.Office.Interop.Outlook.MAPIFolder aFolder) int rootCount = aFolder.Folders.Count; foreach(文件夹中的Microsoft.Office.Interop.Outlook.MAPIFolder子文件夹) { rootCount + = countRootFolders(subfolder); } return rootCount; } – 2011-12-21 19:41:21