c#中是可以操作windows的磁盘配额的。具体方式如下:
- 使用.net framework的WMI类库
示例代码:
using System;
using System.Management;
namespace DiskQuota
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
ManagementClass c = new ManagementClass("Win32_DiskQuota");
ManagementObject quota = c.CreateInstance();
quota["Limit"] = 400000000;
quota["WarningLimit"] = 200000000;
// Get user account object
ManagementObject account = new
ManagementObject("Win32_Account.Domain=TODAY20040216,Name=ASPNET");
account.Get();
// get disk object
ManagementObject disk = new
ManagementObject("Win32_LogicalDisk.DeviceId='F:'");
disk.Get();
quota["QuotaVolume"] = disk;
quota["User"] = account;
quota.Put(); // commit
ManagementClass c = new ManagementClass("Win32_DiskQuota");
Console.WriteLine(c.SystemProperties);
foreach (ManagementObject o in c.GetInstances())
Console.WriteLine("Next : {0}", o.Path);
}
catch (Exception e)
{
Console.WriteLine("error:" + e);
}
}
}
}
2、 直接使用windows的dskquota.dll COM类
vs里面导入引用这个dll:tlbimp %windir%\dskquota.dll /out:c:\DiskQuotaTypeLibrary.dll /namespace:DiskQuotaTypeLibrary
示例代码:
using System;
using DiskQuotaTypeLibrary;
namespace Ex3cut3.Libraries
{
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;
}
}
}