1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Management;
namespace Win32_LogicalDisk
{
class LogicalDisk
{
#region CONSTANTES
const ulong GIGA_OCTETS = 1 << 30;
const ulong MEGA_OCTETS = 1 << 20;
const ulong KILO_OCTETS = 1 << 10;
#endregion
static void Main(string[] args)
{
#region BLOC_TRY
try
{
ManagementPath mgmtPath = new ManagementPath("Win32_LogicalDisk");
ManagementClass classObj = new ManagementClass(null, mgmtPath, null);
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(@"SELECT Name, FreeSpace, Size, VolumeName
FROM Win32_LogicalDisk
WHERE DriveType=3");
ManagementObjectCollection objCollection = objSearcher.Get();
#region VARIABLES_ENVIRONNEMENT
int i = 0; //
Hashtable freeSpace = new Hashtable(); // Variables
Hashtable useSpace = new Hashtable(); // d'environnement
Hashtable capacity = new Hashtable(); //
string[] sLogicalDisk = new string[objCollection.Count]; // Tableau de 'string' contenant les noms de chaque disque
object[] parameters = new object[1]; // ScheduleAutoChk() n'a qu'un seul paramètre
#endregion
// Parcour de tous les disques logiques présent sur la machine
foreach (ManagementObject aObjet in objCollection)
{
#region INFOS_SAUVEGARDEES
// Sauvegarde du nom du disque
sLogicalDisk[i] = aObjet["Name"].ToString();
freeSpace.Add("octet", Convert.ToUInt64(aObjet["FreeSpace"])); //
capacity.Add("octet", Convert.ToUInt64(aObjet["Size"])); // Valeurs en octets
useSpace.Add("octet", Convert.ToUInt64(capacity["octet"]) - Convert.ToUInt64(freeSpace["octet"])); //
freeSpace.Add("kilo_octet", (Convert.ToUInt64(aObjet["FreeSpace"]) / KILO_OCTETS)); //
capacity.Add("kilo_octet", (Convert.ToUInt64(aObjet["Size"]) / KILO_OCTETS)); // Valeurs en kilo octets
useSpace.Add("kilo_octet", Convert.ToUInt64(capacity["kilo_octet"]) - Convert.ToUInt64(freeSpace["kilo_octet"])); //
freeSpace.Add("mega_octet", (Convert.ToUInt64(aObjet["FreeSpace"]) / MEGA_OCTETS)); //
capacity.Add("mega_octet", (Convert.ToUInt64(aObjet["Size"]) / MEGA_OCTETS)); // Valeurs en mega octets
useSpace.Add("mega_octet", Convert.ToUInt64(capacity["mega_octet"]) - Convert.ToUInt64(freeSpace["mega_octet"])); //
freeSpace.Add("giga_octet", (Convert.ToUInt64(aObjet["FreeSpace"]) / GIGA_OCTETS)); //
capacity.Add("giga_octet", (Convert.ToUInt64(aObjet["Size"]) / GIGA_OCTETS)); // Valeurs en giga octets
useSpace.Add("giga_octet", Convert.ToUInt64(capacity["giga_octet"]) - Convert.ToUInt64(freeSpace["giga_octet"])); //
#endregion
#region AFFICHAGE_INFORMATIONS
Console.WriteLine("----------------------------START INFORMATION--------------------------------");
// Affichage des informations 'essentielles' du disque
Console.WriteLine("\n\t\t\t\tName: {0}", aObjet["VolumeName"]);
Console.WriteLine(
String.Format("\nFreeSpace:\n\t\t{0}Go\n\t\t{1}Mo\n\t\t{2}Ko",
freeSpace["giga_octet"], freeSpace["mega_octet"], freeSpace["kilo_octet"], freeSpace["octet"]));
Console.WriteLine(
String.Format("\nUseSpace:\n\t\t{0}Go\n\t\t{1}Mo\n\t\t{2}Ko",
useSpace["giga_octet"], useSpace["mega_octet"], useSpace["kilo_octet"], useSpace["octet"]));
Console.WriteLine(
String.Format("\nCapacity:\n\t\t{0}Go\n\t\t{1}Mo\n\t\t{2}Ko\n",
capacity["giga_octet"], capacity["mega_octet"], capacity["kilo_octet"], capacity["octet"]));
#endregion
freeSpace.Clear(); //
useSpace.Clear(); // on vide les informations
capacity.Clear(); //
i++;
}
#region INVOKE_METHOD_ScheduleAutoChk
parameters[0] = sLogicalDisk;
uint result = Convert.ToUInt32(classObj.InvokeMethod("ScheduleAutoChk", parameters));
#endregion
#region GESTION_ERREURS
// Gestion des différentes erreurs
switch (result)
{
case 0: Console.WriteLine("SUCCESS");
break;
case 1: Console.WriteLine("ERROR REMOTE DRIVE");
break;
case 2: Console.WriteLine("ERROR REMOVABLE DRIVE");
break;
case 3: Console.WriteLine("ERROR DRIVE NO ROOT DIRECTORY");
break;
case 4: Console.WriteLine("ERROR UNKNOW DRIVE");
break;
default: Console.WriteLine("ERROR UNKNOW");
break;
}
#endregion
Console.ReadKey(true);
}
#endregion
#region BLOC_CATCH
catch (ManagementException Ex)
{
Console.WriteLine(String.Format("\n\nMANAGEMENT EXCEPTION INTERCEPTEE\n\t\t{0}\n\n", Ex.Message));
}
catch (Exception Ex)
{
Console.WriteLine(String.Format("\n\nEXCEPTION INTERCEPTEE\n\t\t{0}\n\n", Ex.Message));
}
#endregion
}
}
} |
Partager