磁盘配额,通过ManagementClass("Win32_DiskQuota")

2021-03-15 03:33

阅读:479

标签:ace   sed   format   int   system   generic   warning   span   domain   

C#

using System;
using System.Collections.Generic;
using System.Management;

namespace ConsoleApp2
{
    class Win32DiskQuota
    {
        private static readonly ManagementClass Win32_DiskQuota = new ManagementClass("Win32_DiskQuota");

        internal class DiskQuota
        {
            private readonly string split_sign_1 = "\",Name=\"";
            private readonly string split_sign_2 = "Win32_Account.Domain=\"";
            private readonly string split_sign_3 = "Win32_LogicalDisk.DeviceID=\"";
            internal long DiskSpaceUsed { get; }
            internal long Limit { get; }
            internal string QuotaVolume { get; }
            internal int Status { get; }
            internal string User { get; }
            internal long WarningLimit { get; }

            internal string Username { get; }
            internal string Domain { get; }
            internal string DeviceID { get; }
            internal string Line { get; }

            internal DiskQuota(PropertyDataCollection oProperties)
            {
                DiskSpaceUsed = Convert.ToInt64(oProperties["DiskSpaceUsed"].Value);
                Limit = Convert.ToInt64(oProperties["Limit"].Value);
                QuotaVolume = oProperties["QuotaVolume"].Value.ToString();
                Status = Convert.ToInt32(oProperties["Status"].Value);
                User = oProperties["User"].Value.ToString();
                WarningLimit = Convert.ToInt64(oProperties["WarningLimit"].Value);
                {
                    int i = User.IndexOf(split_sign_1);
                    if (i >= 0)
                    {
                        int pos = i + split_sign_1.Length;
                        Username = User.Substring(pos, User.Length - pos - 1);
                        int len1 = split_sign_2.Length;
                        Domain = User.Substring(len1, i - len1);
                    }
                }
                {
                    int i = QuotaVolume.IndexOf(split_sign_3);
                    if (i == 0)
                    {
                        int len1 = split_sign_3.Length;
                        DeviceID = QuotaVolume.Substring(len1, QuotaVolume.Length - len1 - 1);
                    }
                }
                Line = string.Format("{0} {1}\\{2} {3}/{4}", DeviceID, Domain, Username, DiskSpaceUsed, Limit);
            }
        }
        internal static List GetAllUserDiskQuota()
        {
            List ret = new List();
            ManagementObjectCollection ins = Win32_DiskQuota.GetInstances();
            foreach (ManagementObject o in ins)
            {
                DiskQuota diskQuota = new DiskQuota(o.Properties);
                ret.Add(diskQuota);
            }
            return ret;
        }

        internal static void SetDiskQuota(string DeviceId, string UserName, int LimitGB)
        {
            ManagementObject oQuota = Win32_DiskQuota.CreateInstance();
            if (LimitGB 0)
            {
                //oQuota["Status"] = 0;
                oQuota["Limit"] = 1 * Math.Pow(1024, 4);//次幂1:K,2:M,3:G,4:T,5:P
                oQuota["WarningLimit"] = Convert.ToInt64(10 * Math.Pow(1024, 3));
            }
            else
            {
                long limitByte = Convert.ToInt64(LimitGB * Math.Pow(1024, 3));
                oQuota["Limit"] = limitByte;
                oQuota["WarningLimit"] = limitByte / 2;
            }
            ManagementObject account = new ManagementObject("Win32_Account.Domain=\"" + Environment.MachineName + "\",Name=\"" + UserName + "\"");
            account.Get();
            //// get disk object
            ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceId=\"" + DeviceId + "\"");
            disk.Get();
            oQuota["QuotaVolume"] = disk;
            oQuota["User"] = account;
            oQuota.Put();
        }
    }
}

 

磁盘配额,通过ManagementClass("Win32_DiskQuota")

标签:ace   sed   format   int   system   generic   warning   span   domain   

原文地址:https://www.cnblogs.com/xiangxisheng/p/12445005.html


评论


亲,登录后才可以留言!