使用 SharePoint 2007 中的子文件夹确定网站集和子网站的文件大小

Determine file size for sitecollections and subsites using subfolders in SharePoint 2007

此程序旨在遍历我的所有 Web 应用程序,深入到所有网站集和子网站,并确定每个网站集和子网站的 space。如果我一次手动查询 1 个网站集,一切正常,但如果我尝试以编程方式让它发生,我会不断收到算术溢出异常。有人可以查看我的代码并解释为什么此异常仅在尝试所有 Web 应用程序时发生,而不是在询问单个网站集时发生吗?

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SiteSize
{
    class Program
    {
        static void Main(string[] args)
        {
            long siteCollectionSize = 0;

            string baseUrl = "http://intranet";

            Console.WriteLine("Base Url: " + baseUrl + " (Change baseUrl to list sites starting with)");
            foreach (SPWebApplication mainSite in SPWebService.ContentService.WebApplications)
            {
                foreach (SPSite siteCollections in mainSite.Sites)
                {
                    foreach (SPWeb web in siteCollections.AllWebs)
                    {
                        long webSize = GetSPFolderSize(web.RootFolder) + web.RecycleBin.Cast<SPRecycleBinItem>().Sum(r => r.Size);

                        if (web.Url.StartsWith(baseUrl))
                        {
                            Console.WriteLine(string.Format("({0} {1}", web.Url, FormatSize(webSize)));
                            siteCollectionSize += webSize;
                        }
                    }
                }
            }
            Console.WriteLine("Total Size: " + FormatSize(siteCollectionSize));
        }



        public static long GetSPFolderSize(SPFolder folder)
        {
            long folderSize = 0;
            foreach (SPFile file in folder.Files)
                folderSize += file.TotalLength + file.Versions.Cast<SPFileVersion>().Sum(f => f.Size);
            folderSize += folder.SubFolders.Cast<SPFolder>().Sum(sf => GetSPFolderSize(sf));
            return folderSize;
        }

        public static string FormatSize(long size)
        {

            if (size > Math.Pow(1024, 3))
                return (size / Math.Pow(1024, 3)).ToString("#,#.##") + " GB";

            else if (size > Math.Pow(1024, 2))

                return (size / Math.Pow(1024, 2)).ToString("#,#.##") + " MB";

            else if (size > 1024)
                return (size / 1024).ToString("#,#.##") + " KB";

            else
                return size.ToString("#,#.##") + " Bytes";
        }
    }
}

这里是错误:

Unhandled Exception: System.OverflowException: Arithmetic operation resulted in
an overflow.
   at System.Linq.Enumerable.Sum(IEnumerable`1 source)
   at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)

   at SiteSize.Program.GetSPFolderSize(SPFolder folder)
   at SiteSize.Program.<GetSPFolderSize>b__6(SPFolder sf)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Sum(IEnumerable`1 source)
   at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)

   at SiteSize.Program.GetSPFolderSize(SPFolder folder)
   at SiteSize.Program.<GetSPFolderSize>b__6(SPFolder sf)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Sum(IEnumerable`1 source)
   at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)

   at SiteSize.Program.GetSPFolderSize(SPFolder folder)
   at SiteSize.Program.<>c__DisplayClass2.<getInfo>b__0()
   at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state)
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivile
ges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunEleva
ted secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback sec
ureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevate
d secureCode)
   at SiteSize.Program.getInfo(String site)
   at SiteSize.Program.Main(String[] args)

版本属性 大小是一个整数。就是这个问题。

试试这个...

folderSize += file.TotalLength + file.Versions.Cast<SPFileVersion>().Sum(f => (long)f.Size);