Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Article

Managing Quota Entries in C# and DiskQuotaTypeLibrary

Rate me:
Please Sign up or sign in to vote.
3.26/5 (9 votes)
21 Jun 2004CPOL 70.3K   639   11   17
A simple library to manage the quota entries of the Windows system, works for local and remote computers

Introduction

I was developing a platform for an ISP, and I needed to create a library to manage the quota entries of the users. We have decided to use the Windows quota management, that is considerably better in Windows 2003. I had lots of problems trying to find something that worked, so I decided to post the library completely done and working, and so, here it is for everyone that needs this. If you have any comments or something to add, or possible bugs, just email me...

Code Listing

C#
using System;

using DiskQuotaTypeLibrary;

namespace Ex3cut3.Libraries
{
    /// <summary>
    ///
    /// </summary>
    public class QuotaClass
    {
        private DiskQuotaControlClass _diskQuotaControl;

        //This path has to be in this format or 
        //else is going to give an error of invalid path
        private const string FILESHAREVOLUME = @"\\server\c$\";
        private const int MBTOBYTES = 1048576;

        public DiskQuotaControlClass DiskQuotaControl
        {
            get
            {
                if(this._diskQuotaControl == null)
                {
                    this._diskQuotaControl = new DiskQuotaControlClass();
                    //Initializes the control to the specified path
                    this._diskQuotaControl.Initialize(FILESHAREVOLUME, true);
                }
                return this._diskQuotaControl;
            }
        }

        public QuotaClass()
        {
        }

        /// <summary>
        /// Adds a user to the quota entries of the volume
        /// </summary>
        /// <PARAM name="userName">The name of the user to Add</PARAM>
        /// <PARAM name="userDomain">The domain that the user uses to logon</PARAM>
        /// <PARAM name="quotaLimit">The quota limit of this user</PARAM>
        public void Add(string userName, string userDomain, int quotaLimit)
        {
            DIDiskQuotaUser dskuser = null;
            //In some cases if you use name resolution, 
            //the application will give an error.
            this.DiskQuotaControl.UserNameResolution = 
              UserNameResolutionConstants.dqResolveNone;
            //Here we had the user to the Quotas Entrys of 
            //the volume, we use user@domain.net the logon name of the user.
            //You can use the domain\user or just the user, 
            //but this works faster
            dskuser = this.DiskQuotaControl.AddUser(
              user.SAMAccountName + "@" + user.Domain);
            //here we set the limit of the quota, this is 
            //prepared to receive MB so i convert it to BYTES
            dskuser.QuotaLimit = (int)quotaLimit * MBTOBYTES;
            //here is the set of the warning limit
            dskuser.QuotaThreshold = (int)(quotaLimit / 1.2) * MBTOBYTES;
            dskuser = null;
        }

        /// <summary>
        /// Removes the user form the Quota Entries List
        /// </summary>
        /// <PARAM name="userName"></PARAM>
        public void Remove(string userName)
        {
            //Here we just use the user, and invoke 
            //the method DeleteUser from the control
            this.DiskQuotaControl.DeleteUser(this.GetUser(userName));
        }

        /// <summary>
        /// A private function to return the user object
        /// </summary>
        /// <PARAM name="userName">The user name</PARAM>
        /// <returns>A DIDiskQuotaUser Object 
        /// of the specified user</returns>
        private DIDiskQuotaUser GetUser(string userName)
        {
            //Invokes the method to find a user in a quota entry list
            return this.DiskQuotaControl.FindUser(userName);
        }

        /// <summary>
        /// Gets the quota of the user
        /// </summary>
        /// <PARAM name="userName">The user name</PARAM>
        /// <returns>A formatted string of the quota 
        /// limit of the user</returns>
        private string GetQuota(string userName)
        {
            //here we return the text of the quota limit
            //0.0 bytes, 0.0 Kb, 0.0 Mb etc
            return this.GetUser(userName).QuotaLimitText;
        }

        /// <summary>
        /// Gets the quota currently used by the user
        /// </summary>
        /// <PARAM name="userName">The user name </PARAM>
        /// <returns>A formatted string of the quota 
        /// used by the user</returns>
        private string GetQuotaUsed(string userName)
        {
            return this.GetUser(userName).QuotaUsedText;
        }

        /// <summary>
        /// Change the quota of a specified user
        /// </summary>
        /// <PARAM name="userName">The user name</PARAM>
        /// <PARAM name="quotaLimit">The new quota limit of the user</PARAM>
        private void ChangeQuota(string userName, int quotaLimit)
        {
            DIDiskQuotaUser dskuser = this.GetUser(userName);
            dskuser.QuotaLimit = quotaLimit * Support.Constants.CONVERTBTOMB;
            dskuser.QuotaThreshold = (quotaLimit / 1.2) 
              * Support.Constants.CONVERTBTOMB;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioninvalid path Pin
Member 846946623-Oct-12 4:02
Member 846946623-Oct-12 4:02 
Generalthanks! works like a charm! Pin
tomtom19803-Aug-10 0:49
tomtom19803-Aug-10 0:49 
GeneralSome corrections Pin
dazipe20-Feb-09 14:59
dazipe20-Feb-09 14:59 
GeneralRe: Some corrections Pin
tomtom19808-Aug-10 22:34
tomtom19808-Aug-10 22:34 
GeneralServer 2003 R2 Pin
phsteele26-Oct-07 6:52
phsteele26-Oct-07 6:52 
I came across your DLL library for managing quotas in a Windows 2003 environment. Have you ever extended the library to work with the newer quota features introduced in Windows Server 2003 R2?

Generalplease help me for use this application in vb.net and solve the error The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B) Pin
ram krishna pattnayak19-Jun-07 4:52
ram krishna pattnayak19-Jun-07 4:52 
QuestionQuery Information as non-admin? Pin
Member 111622787-Aug-05 15:43
Member 111622787-Aug-05 15:43 
GeneralError in this article Pin
Member 113378520-Jun-04 21:16
Member 113378520-Jun-04 21:16 
GeneralRe: Error in this article Pin
Feyd-Rauth16-Mar-05 12:56
Feyd-Rauth16-Mar-05 12:56 
GeneralError in this article Pin
Member 113378520-Jun-04 21:15
Member 113378520-Jun-04 21:15 
GeneralThe DLL File Pin
ex3cut3_225-May-04 6:14
ex3cut3_225-May-04 6:14 
GeneralDiskQuotaTypeLibrary Pin
forget_cz19-May-04 21:47
forget_cz19-May-04 21:47 
GeneralRe: DiskQuotaTypeLibrary Pin
Anmelka25-May-04 6:01
Anmelka25-May-04 6:01 
GeneralRe: DiskQuotaTypeLibrary Pin
Member 113378521-Jun-04 21:28
Member 113378521-Jun-04 21:28 
GeneralRe: DiskQuotaTypeLibrary Pin
DaRedmondChew16-Sep-04 10:07
DaRedmondChew16-Sep-04 10:07 
GeneralDiskQuotaTypeLibrary Pin
Member 35614321-Apr-04 3:47
Member 35614321-Apr-04 3:47 
GeneralRe: DiskQuotaTypeLibrary Pin
forget_cz11-Nov-18 13:19
forget_cz11-Nov-18 13:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.