C# DiskQuota类 及引用类

引用 Interop.DiskQuotaTypeLibrary.dll

public class DiskQuota
{

    /// <summary>
    /// 查询用户配额是否设置
    /// </summary>
    /// <param name="Account">用户名</param>
    /// <param name="Disk">盘符</param>
    /// <returns></returns>
    public static bool QuotaExists(string Account, string Disk)
    {
        DiskQuotaControlClass quotaControlClass = DiskQuota.QuotaControl(Disk);
        return quotaControlClass.FindUser(quotaControlClass.TranslateLogonNameToSID(Account)) != null;
    }


    /// <summary>
    /// 创建用户配额
    /// </summary>
    /// <param name="Account">用户名</param>
    /// <param name="Disk">盘符</param>
    /// <param name="limit">大小</param>
    public static void CreateQuota(string Account,  string Disk, int limit)
    {
        if (!DiskQuota.DiskQuotaIsUp(Disk))
            DiskQuota.WakeUpDiskQuota(Disk, 0L, 0L);
        // Logs.Debug(nameof(DiskQuota), quotavolume);
        DiskQuotaControlClass quotaControlClass = DiskQuota.QuotaControl(Disk);
        quotaControlClass.UserNameResolution = UserNameResolutionConstants.dqResolveNone;
        DIDiskQuotaUser diDiskQuotaUser = quotaControlClass.AddUser(Account);
        diDiskQuotaUser.QuotaLimit = limit * 1048576 == 0 ? -1.0 : (double)(limit * 1048576);
        diDiskQuotaUser.QuotaThreshold = limit * 1048576 == 0 ? -1.0 : (double)(limit * 1048576);
    }


    /// <summary>
    /// 更新用户配额
    /// </summary>
    /// <param name="Account">用户名</param>
    /// <param name="Disk">盘符</param>
    /// <param name="limit">大小</param>
    public static void UpdateQuota(string Account,  string Disk, int limit)
    {
        DiskQuotaControlClass quotaControlClass = DiskQuota.QuotaControl(Disk);
        DIDiskQuotaUser user = quotaControlClass.FindUser(quotaControlClass.TranslateLogonNameToSID(Account));
        if (user == null)
            return;
        user.QuotaLimit = limit * 1048576 == 0 ? -1.0 : (double)(limit * 1048576);
        user.QuotaThreshold = limit * 1048576 == 0 ? -1.0 : (double)(limit * 1048576);
    }

    /// <summary>
    /// 删除用户配额
    /// </summary>
    /// <param name="Account">用户名</param>
    /// <param name="Dsik">盘符</param>
 
    public static void DeleteQuota(string Account,  string Dsik)
    {
        DiskQuotaControlClass quotaControlClass = DiskQuota.QuotaControl(Dsik);
        DIDiskQuotaUser user = quotaControlClass.FindUser(quotaControlClass.TranslateLogonNameToSID(Account));
        if (user == null)
            return;
        quotaControlClass.DeleteUser(user);
    }
    /// <summary>
    /// 启用配额
    /// </summary>
    /// <param name="Disk">盘符</param>
  
    /// <returns></returns>
    public static bool WakeUpDiskQuota(string Disk, long DefaultQuotaLimit, long DefaultQuotaThreshold)
    {
        try
        {
            DiskQuotaControlClass quotaControlClass = new DiskQuotaControlClass();
            quotaControlClass.Initialize(Disk, true);
            quotaControlClass.QuotaState = QuotaStateConstants.dqStateEnforce;
            return true;
        }
        catch
        {
            return false;
        }
    }
    /// <summary>
    /// 配额是否启用
    /// </summary>
    /// <param name="Disk"></param>
    /// <returns></returns>
    public static bool DiskQuotaIsUp(string Disk )
    {
        try
        {
            DiskQuotaControlClass quotaControlClass = new DiskQuotaControlClass();
            quotaControlClass.Initialize(Disk, true);
            return quotaControlClass.QuotaState == QuotaStateConstants.dqStateEnforce;
        }
        catch
        {
            return false;
        }
    }
    /// <summary>
    /// 连接硬盘配额
    /// </summary>
    /// <param name="Disk"></param>
    /// <returns></returns>

    private static DiskQuotaControlClass QuotaControl(string Disk)
    {
        DiskQuotaControlClass quotaControlClass = new DiskQuotaControlClass();
        quotaControlClass.Initialize(Disk, true);
        return quotaControlClass;
    }
    /// <summary>
    /// 检测配额服务是否安装
    /// </summary>
    /// <returns></returns>
    public static bool CheckFileServicesInstallation() => false;
    /// <summary>
    /// 
    /// </summary>
    public DiskQuota()
    {

    }
}

本文为原创文章,转载请注明出处!