从outlook获取文件时按大小获取排序文件夹

时间:2013-02-14 14:18:42

标签: c# visual-studio-2010 outlook

我使用以下代码从outlook中检索所有文件夹:

 public void getFolderPath()
    {
        try
        {
            OutLook.Application oApp = new OutLook.Application();
            OutLook.NameSpace oNS = (OutLook.NameSpace)oApp.GetNamespace("MAPI");
            oNS.Logon(Missing.Value, Missing.Value, false, true);

            foreach (MAPIFolder folder in oNS.Folders)
            {
                GetFolders(folder);
            }

            Marshal.ReleaseComObject(oApp);


        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

这显示了OutLook中列出的从上到下的所有文件夹,有没有一种方法可以显示它们或按照它们的大小按升序排列。

类似于:

foreach (MAPIFolder folder in oNS.Folders.sortbysize())
        {
            GetFolders(folder);
        }

1 个答案:

答案 0 :(得分:0)

不,Outlook中的文件夹集合不可排序 即使您使用扩展MAPi(仅限C ++或Delphi)或Redemption(任何语言),PR_MESSAGE_SIZE属性上的文件夹排序也不起作用:PST提供程序不会公开它,并且Exchange倾向于返回0所有文件夹。
您可以按PR_CONTENT_COUNT属性(文件夹中的消息数)排序,但不能按大小排序。以下脚本(Outlook VBA)使用Redemption按PR_CONTENT_COUNT对文件夹进行排序:

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT 
set Folders = Session.Stores.DefaultStore.IPMRootFolder.Folders
Folders.MAPITable.Sort "http://schemas.microsoft.com/mapi/proptag/0x36020003", true
for each Folder in Folders
  Debug.print Folder.Name & " (" & Folder.Fields("http://schemas.microsoft.com/mapi/proptag/0x36020003") & ")"
next 

更快的版本(它不会打开子文件夹并使用ExecSQL方法)将是

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT 
set Folders = Session.Stores.DefaultStore.IPMRootFolder.Folders
set MAPITable = Folders.MAPITable
MAPITable.Sort "http://schemas.microsoft.com/mapi/proptag/0x36020003", true
set Recordset = MAPITable.ExecSQL("SELECT ""http://schemas.microsoft.com/mapi/proptag/0x3001001E"" AS PR_DISPLAY_NAME, " & _
                                  " ""http://schemas.microsoft.com/mapi/proptag/0x36020003"" AS PR_CONTENT_COUNT " & _
                                  " from Table")
while not Recordset.EOF
    Debug.Print Recordset.Fields("PR_DISPLAY_NAME").Value & " (" & Recordset.Fields("PR_CONTENT_COUNT").Value & ")"
    Recordset.MoveNext
wend