如何获得文件夹的大小与Exchange Web服务2010托管API? [英] How do I get folder size with Exchange Web Services 2010 Managed API?

查看:338
本文介绍了如何获得文件夹的大小与Exchange Web服务2010托管API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用EWS 2010托管API来获取用户的邮箱的总大小。我还没有找到一个Web服务的方法来获取这个数据,所以我想我会尝试计算它。我发现了一个看似适用的问题,在其他网站上关于<一个href="http://social.technet.microsoft.com/forums/en-US/exchangesvrdevelopment/thread/31f9a157-4d19-44bf-ad58-62577915b624/"相对=nofollow>发现邮箱的容量与EWS 2007 的,但无论是我不理解什么它要求我做的,或者说方法只是不与EWS 2010年工作。

I'm attempting to use EWS 2010 Managed API to get the total size of a user's mailbox. I haven't found a web service method to get this data, so I figured I would try to calculate it. I found one seemingly-applicable question on another site about finding mailbox sizes with EWS 2007, but either I'm not understanding what it's asking me to do, or that method just doesn't work with EWS 2010.

在code洞察力Noodling身边,我能写什么,我的想法是,会遍历文件夹结构递归,并导致其总和为收件箱里面的所有文件夹的方法:

Noodling around in the code insight, I was able to write what I thought was a method that would traverse the folder structure recursively and result in a combined total for all folders inside the Inbox:

private int traverseChildFoldersForSize(Folder f)
{
    int folderSizeSum = 0;
    if (f.ChildFolderCount > 0)
    {
        foreach (Folder c in f.FindFolders(new FolderView(10000)))
        {
            folderSizeSum += traverseChildFoldersForSize(c);
        }
    }

    folderSizeSum += (int)f.ManagedFolderInformation.FolderSize;

    return folderSizeSum;
}

(假设有没有一个给定的文件夹内超过10,000文件夹。图这是一个安全的赌注...)

(Assumes there aren't more than 10,000 folders inside a given folder. Figure that's a safe bet...)

不幸的是,这是行不通的。

Unfortunately, this doesn't work.

我的递归与此code启动:

I'm initiating the recursion with this code:

Folder root = Folder.Bind(svc, WellKnownFolderName.Inbox);
int totalSize = traverseChildFoldersForSize(root);

但一个空引用异常被抛出,本质上说, [文件夹] .ManagedFolderInformation 是一个空对象引用。

But a Null Reference Exception is thrown, essentially saying that [folder].ManagedFolderInformation is a null object reference.

为了清楚起见,我也试图刚刚得到的根文件夹的大小:

For clarity, I also attempted to just get the size of the root folder:

Console.Write(root.ManagedFolderInformation.FolderSize.ToString());

哪个扔了相同的NRE异常,所以我知道,它不只是一旦你到了一定深度的目录树中ManagedFolderInformation不存在。

Which threw the same NRE exception, so I know that it's not just that once you get to a certain depth in the directory tree that ManagedFolderInformation doesn't exist.

如何获得用户的邮箱的总大小的任何想法?我是不是找错了树?

Any ideas on how to get the total size of the user's mailbox? Am I barking up the wrong tree?

推荐答案

使用EWS Managad的API,你可以使用这个code获得邮箱的累计文件夹大小:

Using the EWS Managad APi, you can use this code to get the cumulative folder size of a mailbox:

internal class Program
{
    private static readonly ExtendedPropertyDefinition PidTagMessageSizeExtended = new ExtendedPropertyDefinition(0xe08,
                                                                                                                  MapiPropertyType
                                                                                                                    .Long);

    public static void Main(string[] args)
    {
        var service = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
                      {Credentials = new NetworkCredential("mail", "pw!")};

        service.AutodiscoverUrl("mail", url => true);

        var offset = 0;
        const int pagesize = 12;
        long size = 0;

        FindFoldersResults folders;
        do
        {
            folders = service.FindFolders(WellKnownFolderName.MsgFolderRoot,
                                          new FolderView(pagesize, offset, OffsetBasePoint.Beginning)
                                          {
                                            Traversal = FolderTraversal.Deep,
                                            PropertySet =
                                                new PropertySet(BasePropertySet.IdOnly, PidTagMessageSizeExtended,
                                                                FolderSchema.DisplayName)
                                          });
            foreach (var folder in folders)
            {
                long folderSize;
                if (folder.TryGetProperty(PidTagMessageSizeExtended, out folderSize))
                {
                    Console.Out.WriteLine("{0}: {1:00.00} MB", folder.DisplayName, folderSize/1048576);
                    size += folderSize;
                }
            }
            offset += pagesize;
        } while (folders.MoreAvailable);
        Console.Out.WriteLine("size = {0:0.00} MB", size/1048576);
    }
}

这篇关于如何获得文件夹的大小与Exchange Web服务2010托管API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆