码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-03-08 22:03:40      阅读:106      评论:0      收藏:0      [点我收藏+]

标签: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<DiskQuota> GetAllUserDiskQuota()
        {
            List<DiskQuota> ret = new List<DiskQuota>();
            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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!