Ce programme recupere les informations des differents lecteurs d'un ordinateur :
[ nom, type, systeme de fichier, espace disque ... ]
Realise dans le cadre du cours Correction de Programmes du Cegep de Chicoutimi.
Le but etait de trouver un programme sur cppfrance et d'y apporter des modifications :
FREEDISKSPACE :
http://www.cppfrance.com/code.aspx?id=10993 ( ironnyc )
Autre source :
http://www.cppfrance.com/code.aspx?id=9421 ( zorque )
Source / Exemple :
/*******************************************************/
/* Projet : "diskSpace"
/* Auteur : Olivier Pilotte
/* E-mail : olivierpilotte@gmail.com
/*
/* Description :
/* Recupere les informations des lecteurs :
/* nom, type, fileSystem, espace disque ...
/*
/* Realisation :
/* Cegep de Chicoutimi : Correction de Programmes
/*
/* Date : 27 Mai 2005
/*******************************************************/
#include <stdio.h>
#include <windows.h>
#include <lmcons.h>
struct GETDISKINFOS // structure de lecteur
{
// lettre / nom / file system
char diskLetter[ 1 ],
diskName[ UNLEN + 1 ],
systemName[ UNLEN + 1 ];
LPTSTR diskType; // type
// no serie, max, flag
LPDWORD diskSerial,
diskMax,
fileSystem;
// espace total / libre / utilisee
TCHAR totalSpace[ 15 ],
freeSpace[ 15 ],
usedSpace[ 15 ];
// bytes / secteurs / clusters
unsigned long sectorCluster,
bytesSector,
freeCluster,
totalCluster;
};
namespace diskSpace
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1( void )
{
InitializeComponent();
}
protected:
void Dispose( Boolean disposing )
{
if ( disposing && components )
{
components->Dispose();
}
__super::Dispose( disposing );
}
// definition des attributs de l'interface
private: System::Windows::Forms::Label * label1;
private: System::Windows::Forms::Label * label2;
private: System::Windows::Forms::Label * label3;
private: System::Windows::Forms::Label * label4;
private: System::Windows::Forms::Label * label5;
private: System::Windows::Forms::Label * label6;
private: System::Windows::Forms::Label * label7;
private: System::Windows::Forms::Label * label8;
private: System::Windows::Forms::Label * label9;
private: System::Windows::Forms::Label * label10;
private: System::Windows::Forms::Label * label11;
private: System::Windows::Forms::Label * statusBar;
private: System::Windows::Forms::GroupBox * groupBox1;
private: System::Windows::Forms::GroupBox * groupBox2;
private: System::Windows::Forms::GroupBox * groupBox3;
private: System::Windows::Forms::MainMenu * mainMenu1;
private: System::Windows::Forms::MenuItem * menuItem1;
private: System::Windows::Forms::MenuItem * menuItem3;
private: System::Windows::Forms::MenuItem * menuItem4;
private: System::Windows::Forms::TextBox * totalSpaceBox;
private: System::Windows::Forms::TextBox * usedSpaceBox;
private: System::Windows::Forms::TextBox * freeSpaceBox;
private: System::Windows::Forms::TextBox * bytesBox;
private: System::Windows::Forms::TextBox * sectorBox;
private: System::Windows::Forms::TextBox * freeClusterBox;
private: System::Windows::Forms::TextBox * totalClusterBox;
private: System::Windows::Forms::TextBox * typeBox;
private: System::Windows::Forms::TextBox * nameBox;
private: System::Windows::Forms::TextBox * sysBox;
private: System::Windows::Forms::ComboBox * comboBox1;
private: static Object *diskArray[] = { S"a:", S"b:", S"c:", S"d:",
S"e:", S"f:", S"g:", S"h:",
S"i:", S"j:", S"k:", S"l:",
S"m:", S"n:", S"o:", S"p:",
S"q:", S"r:", S"s:", S"t:",
S"u:", S"v:", S"w:", S"x:",
S"y:", S"z:" };
private: System::ComponentModel::Container * components;
// creation des attributs de l'interface
void InitializeComponent(void)
{
this->label1 = new System::Windows::Forms::Label();
this->label2 = new System::Windows::Forms::Label();
this->label3 = new System::Windows::Forms::Label();
this->label4 = new System::Windows::Forms::Label();
this->label5 = new System::Windows::Forms::Label();
this->label6 = new System::Windows::Forms::Label();
this->label7 = new System::Windows::Forms::Label();
this->label8 = new System::Windows::Forms::Label();
this->label9 = new System::Windows::Forms::Label();
this->label10 = new System::Windows::Forms::Label();
this->label11 = new System::Windows::Forms::Label();
this->statusBar = new System::Windows::Forms::Label();
this->groupBox1 = new System::Windows::Forms::GroupBox();
this->usedSpaceBox = new System::Windows::Forms::TextBox();
this->totalSpaceBox = new System::Windows::Forms::TextBox();
this->freeSpaceBox = new System::Windows::Forms::TextBox();
this->groupBox2 = new System::Windows::Forms::GroupBox();
this->totalClusterBox = new System::Windows::Forms::TextBox();
this->freeClusterBox = new System::Windows::Forms::TextBox();
this->sectorBox = new System::Windows::Forms::TextBox();
this->bytesBox = new System::Windows::Forms::TextBox();
this->groupBox3 = new System::Windows::Forms::GroupBox();
this->sysBox = new System::Windows::Forms::TextBox();
this->nameBox = new System::Windows::Forms::TextBox();
this->typeBox = new System::Windows::Forms::TextBox();
this->mainMenu1 = new System::Windows::Forms::MainMenu();
this->menuItem1 = new System::Windows::Forms::MenuItem();
this->menuItem3 = new System::Windows::Forms::MenuItem();
this->menuItem4 = new System::Windows::Forms::MenuItem();
this->comboBox1 = new System::Windows::Forms::ComboBox();
this->groupBox1->SuspendLayout();
this->groupBox2->SuspendLayout();
this->groupBox3->SuspendLayout();
this->SuspendLayout();
//
// label1
//
this->label1->Location = System::Drawing::Point(8, 24);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing::Size(106, 19);
this->label1->TabIndex = 0;
this->label1->Text = S"Espace Total";
//
// label2
//
this->label2->Location = System::Drawing::Point(8, 80);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing::Size(106, 18);
this->label2->TabIndex = 1;
this->label2->Text = S"Espace Utilise";
//
// label3
//
this->label3->Location = System::Drawing::Point(8, 52);
this->label3->Name = S"label3";
this->label3->Size = System::Drawing::Size(106, 19);
this->label3->TabIndex = 2;
this->label3->Text = S"Espace Libre";
//
// label4
//
this->label4->Location = System::Drawing::Point(8, 80);
this->label4->Name = S"label4";
this->label4->Size = System::Drawing::Size(100, 20);
this->label4->TabIndex = 1;
this->label4->Text = S"Type";
//
// label5
//
this->label5->Location = System::Drawing::Point(8, 52);
this->label5->Name = S"label5";
this->label5->Size = System::Drawing::Size(112, 19);
this->label5->TabIndex = 6;
this->label5->Text = S"Secteurs / Cluster";
//
// label6
//
this->label6->Location = System::Drawing::Point(8, 24);
this->label6->Name = S"label6";
this->label6->Size = System::Drawing::Size(112, 19);
this->label6->TabIndex = 7;
this->label6->Text = S"Bytes / Secteur";
//
// label7
//
this->label7->Location = System::Drawing::Point(8, 80);
this->label7->Name = S"label7";
this->label7->Size = System::Drawing::Size(112, 19);
this->label7->TabIndex = 8;
this->label7->Text = S"Clusters Libres";
//
// label8
//
this->label8->Location = System::Drawing::Point(8, 108);
this->label8->Name = S"label8";
this->label8->Size = System::Drawing::Size(112, 19);
this->label8->TabIndex = 9;
this->label8->Text = S"Clusters Total";
//
// label9
//
this->label9->Location = System::Drawing::Point(8, 24);
this->label9->Name = S"label9";
this->label9->Size = System::Drawing::Size(100, 20);
this->label9->TabIndex = 2;
this->label9->Text = S"Nom Volume";
//
// label10
//
this->label10->Location = System::Drawing::Point(8, 52);
this->label10->Name = S"label10";
this->label10->Size = System::Drawing::Size(100, 20);
this->label10->TabIndex = 3;
this->label10->Text = S"File System";
//
// label11
//
this->label11->Location = System::Drawing::Point(88, 12);
this->label11->Name = S"label11";
this->label11->Size = System::Drawing::Size(168, 16);
this->label11->TabIndex = 13;
this->label11->Text = S"Informations sur le Lecteur";
//
// statusBar
//
this->statusBar->Location = System::Drawing::Point(12, 256);
this->statusBar->Name = S"statusBar";
this->statusBar->Size = System::Drawing::Size(184, 20);
this->statusBar->TabIndex = 14;
//
// groupBox1
//
this->groupBox1->Controls->Add(this->usedSpaceBox);
this->groupBox1->Controls->Add(this->label2);
this->groupBox1->Controls->Add(this->label3);
this->groupBox1->Controls->Add(this->totalSpaceBox);
this->groupBox1->Controls->Add(this->freeSpaceBox);
this->groupBox1->Controls->Add(this->label1);
this->groupBox1->Location = System::Drawing::Point(8, 140);
this->groupBox1->Name = S"groupBox1";
this->groupBox1->Size = System::Drawing::Size(192, 108);
this->groupBox1->TabIndex = 6;
this->groupBox1->TabStop = false;
//
// usedSpaceBox
//
this->usedSpaceBox->Location = System::Drawing::Point(116, 76);
this->usedSpaceBox->Name = S"usedSpaceBox";
this->usedSpaceBox->Size = System::Drawing::Size(67, 22);
this->usedSpaceBox->TabIndex = 4;
this->usedSpaceBox->Text = S"";
//
// totalSpaceBox
//
this->totalSpaceBox->Location = System::Drawing::Point(116, 20);
this->totalSpaceBox->Name = S"totalSpaceBox";
this->totalSpaceBox->Size = System::Drawing::Size(67, 22);
this->totalSpaceBox->TabIndex = 3;
this->totalSpaceBox->Text = S"";
//
// freeSpaceBox
//
this->freeSpaceBox->Location = System::Drawing::Point(116, 48);
this->freeSpaceBox->Name = S"freeSpaceBox";
this->freeSpaceBox->Size = System::Drawing::Size(67, 22);
this->freeSpaceBox->TabIndex = 5;
this->freeSpaceBox->Text = S"";
//
// groupBox2
//
this->groupBox2->Controls->Add(this->totalClusterBox);
this->groupBox2->Controls->Add(this->freeClusterBox);
this->groupBox2->Controls->Add(this->sectorBox);
this->groupBox2->Controls->Add(this->label8);
this->groupBox2->Controls->Add(this->label7);
this->groupBox2->Controls->Add(this->label6);
this->groupBox2->Controls->Add(this->label5);
this->groupBox2->Controls->Add(this->bytesBox);
this->groupBox2->Location = System::Drawing::Point(208, 140);
this->groupBox2->Name = S"groupBox2";
this->groupBox2->Size = System::Drawing::Size(196, 136);
this->groupBox2->TabIndex = 10;
this->groupBox2->TabStop = false;
//
// totalClusterBox
//
this->totalClusterBox->Location = System::Drawing::Point(120, 104);
this->totalClusterBox->Name = S"totalClusterBox";
this->totalClusterBox->Size = System::Drawing::Size(67, 22);
this->totalClusterBox->TabIndex = 12;
this->totalClusterBox->Text = S"";
//
// freeClusterBox
//
this->freeClusterBox->Location = System::Drawing::Point(120, 76);
this->freeClusterBox->Name = S"freeClusterBox";
this->freeClusterBox->Size = System::Drawing::Size(67, 22);
this->freeClusterBox->TabIndex = 11;
this->freeClusterBox->Text = S"";
//
// sectorBox
//
this->sectorBox->Location = System::Drawing::Point(120, 48);
this->sectorBox->Name = S"sectorBox";
this->sectorBox->Size = System::Drawing::Size(67, 22);
this->sectorBox->TabIndex = 10;
this->sectorBox->Text = S"";
//
// bytesBox
//
this->bytesBox->Location = System::Drawing::Point(120, 20);
this->bytesBox->Name = S"bytesBox";
this->bytesBox->Size = System::Drawing::Size(68, 22);
this->bytesBox->TabIndex = 6;
this->bytesBox->Text = S"";
//
// groupBox3
//
this->groupBox3->Controls->Add(this->sysBox);
this->groupBox3->Controls->Add(this->nameBox);
this->groupBox3->Controls->Add(this->label10);
this->groupBox3->Controls->Add(this->label9);
this->groupBox3->Controls->Add(this->label4);
this->groupBox3->Controls->Add(this->typeBox);
this->groupBox3->Location = System::Drawing::Point(8, 32);
this->groupBox3->Name = S"groupBox3";
this->groupBox3->Size = System::Drawing::Size(396, 108);
this->groupBox3->TabIndex = 12;
this->groupBox3->TabStop = false;
//
// sysBox
//
this->sysBox->Location = System::Drawing::Point(112, 48);
this->sysBox->Name = S"sysBox";
this->sysBox->Size = System::Drawing::Size(136, 22);
this->sysBox->TabIndex = 5;
this->sysBox->Text = S"";
//
// nameBox
//
this->nameBox->Location = System::Drawing::Point(112, 20);
this->nameBox->Name = S"nameBox";
this->nameBox->Size = System::Drawing::Size(136, 22);
this->nameBox->TabIndex = 4;
this->nameBox->Text = S"";
//
// typeBox
//
this->typeBox->Location = System::Drawing::Point(112, 76);
this->typeBox->Name = S"typeBox";
this->typeBox->Size = System::Drawing::Size(136, 22);
this->typeBox->TabIndex = 0;
this->typeBox->Text = S"";
//
// mainMenu1
//
System::Windows::Forms::MenuItem* __mcTemp__1[] = new System::Windows::Forms::MenuItem*[2];
__mcTemp__1[0] = this->menuItem1;
__mcTemp__1[1] = this->menuItem4;
this->mainMenu1->MenuItems->AddRange(__mcTemp__1);
//
// menuItem1
//
this->menuItem1->Index = 0;
System::Windows::Forms::MenuItem* __mcTemp__2[] = new System::Windows::Forms::MenuItem*[1];
__mcTemp__2[0] = this->menuItem3;
this->menuItem1->MenuItems->AddRange(__mcTemp__2);
this->menuItem1->Text = S"Fichier";
//
// menuItem3
//
this->menuItem3->Index = 0;
this->menuItem3->Text = S"Quitter";
//
// menuItem4
//
this->menuItem4->Index = 1;
this->menuItem4->Text = S"A Propos";
//
// comboBox1
this->comboBox1->Location = System::Drawing::Point(8, 8);
this->comboBox1->Name = S"comboBox1";
this->comboBox1->Size = System::Drawing::Size(72, 24);
this->comboBox1->TabIndex = 0;
//
// Form1
this->AutoScaleBaseSize = System::Drawing::Size(6, 15);
this->ClientSize = System::Drawing::Size(410, 281);
this->Controls->Add(this->statusBar);
this->Controls->Add(this->label11);
this->Controls->Add(this->groupBox3);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->groupBox1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->MaximizeBox = false;
this->Menu = this->mainMenu1;
this->Name = S"Form1";
this->Text = S"diskSpace";
this->Load += new System::EventHandler(this, Form1_Load);
this->groupBox1->ResumeLayout(false);
this->groupBox2->ResumeLayout(false);
this->groupBox3->ResumeLayout(false);
this->ResumeLayout(false);
}
// form1 load
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
this->comboBox1->add_SelectedIndexChanged( new EventHandler( this, comboBox1_action ) );
this->menuItem3->add_Click( new EventHandler( this, menuItem3_quit ) );
this->menuItem4->add_Click( new EventHandler( this, menuItem4_about ) );
comboBox1->Items->AddRange( diskArray );
}
// combobox1 selection
private: System::Void comboBox1_action( System::Object * sender, System::EventArgs * e )
{
diskInformations( comboBox1->get_SelectedIndex() );
}
// menu quitter
private: System::Void menuItem3_quit( System::Object * sender, System::EventArgs * e )
{
exit( 0 );
}
// menu a propos
private: System::Void menuItem4_about( System::Object * sender, System::EventArgs * e )
{
statusBar->set_Text( " A Propos ... " );
}
// disk informations
private: System::Void diskInformations( int index )
{
GETDISKINFOS diskInfos; // structure diskInfos
resetBoxes(); // vide interface
char theDisk[ 1 ]; // lecteur
HRESULT drvType; // type
__int64 available,
total,
free,
used;
DWORD volSize = UNLEN + 1;
DWORD sysSize = UNLEN + 1;
// recupere la lettre du lecteur selectionne
String *disk = diskArray[ index ]->ToString();
for( int cpt = 0; cpt < disk->Length; cpt++ )
{
theDisk[ cpt ] = disk->get_Chars( cpt );
}
sprintf( diskInfos.diskLetter, theDisk );
// recupere le type du disque
drvType = GetDriveType( theDisk );
if( drvType == 1 ) // aucun disque present
{
resetBoxes();
statusBar->set_Text( "disque inexistant !" );
}
else // disque present
{
statusBar->set_Text( "" );
// determine le type du disque
switch( drvType )
{
case 1 : diskInfos.diskType = "Disque Inexistant !"; break;
case 2 : diskInfos.diskType = "Lecteur de Disquette"; break;
case 3 : diskInfos.diskType = "Disque Dur"; break;
case 4 : diskInfos.diskType = "Lecteur amovible/reseaux"; break;
case 5 : diskInfos.diskType = "Lecteur CD/DVD"; break;
case 6 : diskInfos.diskType = "Disque Virtuel"; break;
}
// recupere les informations du disque
GetVolumeInformation( diskInfos.diskLetter,
diskInfos.diskName,
volSize,
diskInfos.diskSerial,
diskInfos.diskMax,
diskInfos.fileSystem,
diskInfos.systemName,
sysSize
);
//
GetDiskFreeSpace( theDisk,
&diskInfos.sectorCluster,
&diskInfos.bytesSector,
&diskInfos.freeCluster,
&diskInfos.totalCluster
);
//
GetDiskFreeSpaceEx( theDisk,
(PULARGE_INTEGER)&available,
(PULARGE_INTEGER)&total,
(PULARGE_INTEGER)&free
);
// calcul de l'espace
total = ( total / 1024 ) / 1024;
free = ( free / 1024 ) / 1024;
used = total - free;
// inscrit les resultats
_ui64toa( total, diskInfos.totalSpace, 10);
_ui64toa( free, diskInfos.freeSpace, 10);
_ui64toa( used, diskInfos.usedSpace, 10);
// affiche les informations du disque
nameBox->set_Text( diskInfos.diskName );
sysBox->set_Text( diskInfos.systemName );
typeBox->set_Text( diskInfos.diskType );
//
totalSpaceBox->set_Text( diskInfos.totalSpace );
freeSpaceBox->set_Text( diskInfos.freeSpace );
usedSpaceBox->set_Text( diskInfos.usedSpace );
//
bytesBox->set_Text( diskInfos.bytesSector.ToString() );
sectorBox->set_Text( diskInfos.sectorCluster.ToString() );
freeClusterBox->set_Text( diskInfos.freeCluster.ToString() );
totalClusterBox->set_Text( diskInfos.totalCluster.ToString() );
}
}
// efface informations
private: System::Void resetBoxes()
{
nameBox->set_Text( "" );
typeBox->set_Text( "" );
sysBox->set_Text( "" );
totalSpaceBox->set_Text( "" );
freeSpaceBox->set_Text( "" );
usedSpaceBox->set_Text( "" );
bytesBox->set_Text( "" );
sectorBox->set_Text( "" );
freeClusterBox->set_Text( "" );
totalClusterBox->set_Text( "" );
}
};
}
Conclusion :
Merci a ironnyc et zorque pour le code !
( fichier executable dans le .zip )
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.