|
begin process at 2008 07 20 15:50:36
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
UTILISATION WMI ET WIN32_LOGICALDISK
Information sur la source
Description
Ce bout de code permet de récupérer diverses informations sur les disques durs d'un PC, vi WMI et la classe Win32_LogicalDisk. Par exemple, il affiche la capacité totale du disque, l'espace utilisé et l'espace libre restant. De plus, j'ai rajouté l'utilisation de la méthode ScheduleAutoChk. Pour plus d'informations sur cette classe et cette méthode, je vous conseille la fantastique MSDN.
Source
- 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
- }
- }
- }
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
}
}
}
Conclusion
Pour que ce code fonctionne, il vous suffit d'ajouter la référence System.Management
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 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 | | | |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|
|