磁盘配额,通过ManagementClass("Win32_DiskQuota")
Posted xiangxisheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了磁盘配额,通过ManagementClass("Win32_DiskQuota")相关的知识,希望对你有一定的参考价值。
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")的主要内容,如果未能解决你的问题,请参考以下文章
三十,GeoWebCache 磁盘配额(/diskquota)