使用 Microsoft Outlook 14.0 对象库引用和下面的代码,我想计算邮箱下列出的所有文件夹,包括每个子文件夹,但我遇到了问题。
此代码只计算顶级文件夹和二级文件夹,但不计算下面的任何子文件夹。我在 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;
}
非常感谢任何帮助!!
最佳答案
在我看来你的循环只询问每个第一级文件夹他们有多少文件夹。因此,您可以获得根目录中的文件夹数量,以及每个文件夹下一级的文件夹数量,但它不会遍历文件夹树以询问更多级别它们有多少。
这是一个简单的树遍历问题。
您必须实现一个递归函数来向下遍历文件夹结构以获得准确的计数。
public int countRootFolders(Microsoft.Office.Interop.Outlook.MAPIFolder aFolder)
{
int rootCount = aFolder.Folders.Count;
foreach (Microsoft.Office.Interop.Outlook.MAPIFolder subfolder in aFolder.Folders)
{
rootCount += countRootFolders( subFolder );
}
return rootCount;
}
用你想要计数的根文件夹调用它,它应该非常接近你所追求的。
关于c# - 你如何统计Outlook邮箱下的每个子文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8594955/