2013-05-16 25 views
0

我將內容數據庫的大小限制在200 GB以內。我已經使用下面的代碼來獲取內容數據庫的大小。如何通過C#獲取網站集的大小?

SPWebApplication elevatedWebApp = spWeb.Site.WebApplication; 
SPContentDatabaseCollection dbCollection = elevatedWebApp.ContentDatabases; 
//Guid id = dbCollection[0].Id; 

Guid lastContentDbGuid = new Guid(contentDbGuid); 
//SPContentDatabase lastDatabase = dbCollection[dbCollection.Count - 1]; 

SPContentDatabase lastDatabase = dbCollection[lastContentDbGuid]; 
ulong dbSize = lastDatabase.DiskSizeRequired; 
contentDbMB = ConvertToMegabytes(dbSize); 

static string ConvertToMegabytes(ulong bytes) 
{ 
    return ((decimal)bytes/1024M/1024M).ToString("F1") + "MB"; 
} 

與之相似我想限制網站集高達100 GB。所以我用下面的代碼

long bytes = spSite.Usage.Storage; 
double siteCollectionSizeInMB = ConvertBytesToMegabytes(bytes); 

>

static double ConvertBytesToMegabytes(long bytes) 
{ 
    return (bytes/1024f)/1024f; 
} 

我只用C#需要集合的大小。我做對了嗎?如果我做錯了什麼,請指導我。如果有人有不同的解決方案,那麼請分享。

回答

0

看看這兩個解決方案在這裏:Theres both a powershell and c# solution即C#解決方案,使集合大小爲:

SPWebService service = SPFarm.Local.Services.GetValue<SPWebService>(); 
    foreach (SPWebApplication webapp in service.WebApplications) 
    { 
     Console.WriteLine("WebApplication : " + webapp.Name); 
     foreach (SPContentDatabase db in webapp.ContentDatabases) 
     { 
      Console.WriteLine("{0}, Nb Sites : {1}, Size : {2}, ", db.Name, db.Sites.Count, db.DiskSizeRequired); 
     } 
    } 

你可以使用以下方法來獲得總DB大小等(Taken from here):

Server srv new Server(); 

Database db = srv.Databases("MyDatabase"); 

\\Display size and space information for the database. 
Console.WriteLine("data space usage (KB): " + db.DataSpaceUsage.ToString); 
Console.WriteLine("index space usage (KB): " + db.IndexSpaceUsage.ToString); 
Console.WriteLine("space available (KB): " + db.SpaceAvailable.ToString); 
Console.WriteLine("database size (MB): " + db.Size.ToString); 
0

C#基本上採用相同的結構Powershell的在這種情況下和一個很好的例子可以發現here

using(SPSite site = new SPSite(http://yoursitehere.com/site) 
{ 
    SPSite.UsageInfo usageInfo = site.UsageInfo; 
    long storageReq = usageInfo.Storage; 
} 

如果需要通過在內容數據庫中的每個站點重複,只需調用內容數據庫對象,並引用其.sites財產。