如何通过C#获得网站集的大小?

如何通过C#获得网站集的大小?,c#,sharepoint-2010,size,sitecollection,C#,Sharepoint 2010,Size,Sitecollection,我已将内容数据库的大小限制为200 GB。我使用了以下代码来获取内容数据库的大小 SPWebApplication elevatedWebApp = spWeb.Site.WebApplication; SPContentDatabaseCollection dbCollection = elevatedWebApp.ContentDatabases; //Guid id = dbCollection[0].Id; Guid lastContentDbGuid = new Guid(conte

我已将内容数据库的大小限制为200 GB。我使用了以下代码来获取内容数据库的大小

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);

同样,我想限制一个网站集高达100 GB。因此,我使用以下代码

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


我只需要使用C#的集合大小。我这样做对吗?如果我做错了什么,请指导我。如果有人有不同的解决方案,请分享。

这里回答了这个问题。

看看这里的两个解决方案:即获得集合大小的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);
        }
    }
在本例中,C#使用与Powershell基本相同的结构,可以找到一个很好的示例

如果需要遍历内容数据库中的每个站点,只需调用内容数据库对象并引用其
.sites
属性即可

static double ConvertBytesToMegabytes(long bytes)
{
    return (bytes / 1024f) / 1024f;
}
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);
        }
    }
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);
using(SPSite site = new SPSite(http://yoursitehere.com/site)
{
    SPSite.UsageInfo usageInfo = site.UsageInfo;
    long storageReq = usageInfo.Storage;
}