title: SharePoint get the size of individual web sites using API
body: <p>Right now all I am using to calculate the size are the files in the folders.  I do not think this is all of it, because the content database size is about 15gb.  When I calculate the size of all the files I get around 10gb.  Does anyone know what I may be missing?</p>
  
  <p>Here is the code I have so far.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.SharePoint;
  using System.Globalization;
  
  namespace WebSizeTesting
  {
  class Program
  {
  
  
      static void Main(string[] args)
      {
          long SiteCollectionBytes = 0;
  
          using (SPSite mainSite = new SPSite("http://sharepoint-test"))
          {
              // loop through the websites
              foreach (SPWeb web in mainSite.AllWebs)
              {
  
                  long webBytes = GetSPFolderSize(web.RootFolder);
  
                  // Add in size of each web site's recycle bin
  
                  webBytes += web.RecycleBin.OfType&lt;SPRecycleBinItem&gt;().Select(item =&gt; item.Size).ToArray&lt;long&gt;().Sum();
  
                  Console.WriteLine("Url: {0}, Size: {1}", web.Url, ConvertBytesToDisplayText( webBytes ));
  
                  SiteCollectionBytes += webBytes;
              }
  
  
              long siteCollectionRecycleBinBytes = mainSite.RecycleBin.OfType&lt;SPRecycleBinItem&gt;().Select(item =&gt; item.Size).ToArray&lt;long&gt;().Sum();
  
              Console.WriteLine("Site Collection Recycle Bin: " + ConvertBytesToDisplayText(siteCollectionRecycleBinBytes));
  
              SiteCollectionBytes += siteCollectionRecycleBinBytes;
          }
  
          Console.WriteLine("Total Size: " + ConvertBytesToDisplayText(SiteCollectionBytes));
  
          Console.ReadKey();
      }
  
  
  
      public static long GetSPFolderSize(SPFolder folder)
      {
          long byteCount = 0;
  
          // calculate the files in the immediate folder
          foreach (SPFile file in folder.Files)
          {
              byteCount += file.TotalLength;
  
              // also include file versions
              foreach (SPFileVersion fileVersion in file.Versions)
              {
                  byteCount += fileVersion.Size;
              }
  
          }
  
          // Handle sub folders
          foreach (SPFolder subFolder in folder.SubFolders)
          {
              byteCount += GetSPFolderSize(subFolder);
          }
  
          return byteCount;
      }
  
      public static string ConvertBytesToDisplayText(long byteCount)
      {
          string result = "";
  
          if (byteCount &gt; Math.Pow(1024, 3))
          {
              // display as gb
              result = (byteCount / Math.Pow(1024, 3)).ToString("#,#.##", CultureInfo.InvariantCulture) + " GB";
          }
          else if (byteCount &gt; Math.Pow(1024, 2))
          {
              // display as mb
              result = (byteCount / Math.Pow(1024, 2)).ToString("#,#.##", CultureInfo.InvariantCulture) + " MB";
          }
          else if (byteCount &gt; 1024)
          {
              // display as kb
              result = (byteCount / 1024).ToString("#,#.##", CultureInfo.InvariantCulture) + " KB";
          }
          else
          {
              // display as bytes
              result = byteCount.ToString("#,#.##", CultureInfo.InvariantCulture) + " Bytes";
          }
  
          return result;
      }
  }
  }
  </code></pre>
  
  <p><strong>edit 2:15 pm 3/1/2010 cst</strong>  I added in the ability to count file versions as part of the size to the code.  As was suggested by Goyuix in the post below.  It still is off by a considerable amount of the physical database size.</p>
  
  <p><strong>edit 8:38 am 3/3/2010 cst</strong> I added in the calculating of the recycle bin size for each web, and the site collection recycle bin.  These changes where suggested by ArjanP.  Also i wanted to add, that I am very open to more efficient ways of doing this.</p>