使用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