2015-01-09 61 views
0

此程序用於遍歷我的所有網絡應用程序,深入到所有網站集和子網站並確定每個網站集和子網站的空間。一切工作正常,如果我一次手動詢問1網站集,但如果我試圖讓它發生編程我不斷收到算術溢出異常。是否有人可以查看我的代碼並解釋爲什麼只有在嘗試所有Web應用程序時纔會發生此異常,而不是在單個網站集被詢問時發生?確定SharePoint 2007中使用子文件夾的網站集和子網站的文件大小

下面是代碼:

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) 
+0

我決定手動完成對每個網站集,顯然1個網站,僅僅是太大了。文件大小如何能比長期大? – snapplex 2015-01-10 00:29:00

回答

0

版本屬性Size是一個int。這是問題。

嘗試......

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