Here is an example on how you can interface with FSRM (File Server Resource Manager) to programmatically set or change a quota for a given directory.
First you need to find and reference the srmlib.dll file from either the Windows SDK 7.1 or from Windows 2008 R2 installation.

// you need these using statements.
using Microsoft.Storage;
using System.Runtime.InteropServices; 
using System.IO; // For the Directory creation if needed

// Here comes the code that goes into your main class or wherever you need it.

// Directory to Set/Change the quota of
string directory = @"c:\LimitedSpace";

// If you want to create the directory if it doesn't exist uncomment this section
/*
if (!Directory.Exists(directory))
{
  Directory.CreateDirectory(directory);
}
*/

// I want 512MB quota,  you can replace this with what you want. The quota size is in KB.
int NewQuotaSize = 512 * 1024 * 1024;

// Create our interface
IFsrmQuotaManager FSRMQuotaManager = new FsrmQuotaManagerClass();
IFsrmQuota Quota = null;

try
{
  // First we need to see if there is already a quota on the directory.
  Quota = FSRMQuotaManager.GetQuota(directory);
  // If there is quota then we just set it to our new size
  Quota.QuotaLimit = NewQuotaSize;
}
catch (COMException e)
{
  unchecked
  {
    if (e.ErrorCode == (int)0x80045301)
    {
      // There is no quota on this directory so we need to create it.
      Quota = FSRMQuotaManager.CreateQuota(directory);
      // And then set our desired quota
      Quota.QuotaLimit = NewQuotaSize;
    }
    else
    {
      // some other COM exception occured so we return the error
      Console.WriteLine(e);
      return;
    }
  }
}
catch (Exception e)
{
  // Generic error handling would go here
  Console.WriteLine(e);
  return;
}

// and finally we commit our changes.
Quota.Commit();

3 Responses to C# : Change Folder Quota Programmatically in Windows 2008 R2

  1. chris says:

    I use this code to create quotas and works fine unless I try to create a quota larger than 2048 mb. Creating quotas larger than 2048 mb using the File Server Resource Manager or powershell works fine… any ideas?