J'ai refais le programme en utilisant cette fois ci une classe. Crois-tu que c'était justifié ? Si oui, est elle bien conçue ?
Voici le code de la classe :
namespace Compare
{
class Comparaison
{
//attributs
private string _path1, _path2;
private Hashtable _DicoFiles;
private string[] _Catégories = new string[4];
//constructeur
public Comparaison(string P1, string P2)
{
DirectoryInfo Folder1, Folder2;
int numero = 1;
try
{
Folder1 = new DirectoryInfo(P1);
numero++;
Folder2 = new DirectoryInfo(P2);
}
catch
{
MessageBox.Show("Chemin " + numero + " incorrect !", "ATTENTION !", MessageBoxButtons.OK);
return;
}
FileInfo[] MyFileInfoList1 = Folder1.GetFiles();
FileInfo[] MyFileInfoList2 = Folder2.GetFiles();
Hashtable DICOFILES = new Hashtable();
foreach (FileInfo MyFileInfo in MyFileInfoList1)
DICOFILES.Add(MyFileInfo.Name, 1);
foreach (FileInfo MyFileInfo in MyFileInfoList2)
{
if (DICOFILES.ContainsKey(MyFileInfo.Name))
if (Read(P1 + "\\" + MyFileInfo.Name) == Read(P2 + "\\" + MyFileInfo.Name))
DICOFILES[MyFileInfo.Name] = 3;
else
DICOFILES[MyFileInfo.Name] = 2;
else
DICOFILES.Add(MyFileInfo.Name, 0);
}
IEnumerator clés = DICOFILES.Keys.GetEnumerator();
while (clés.MoveNext())
this._Catégories[(int)DICOFILES[clés.Current]] += clés.Current + Environment.NewLine;
this._DicoFiles = DICOFILES;
this._path1 = P1;
this._path2 = P2;
}
//propriétés
public Hashtable DicoFiles{
get { return _DicoFiles; }
set { _DicoFiles=value; }
}
public string path1{
get { return _path1; }
set { _path1=value; }
}
public string path2{
get { return _path2; }
set { _path2=value; }
}
public string PasDans1{
get { return _Catégories[0]; }
}
public string PasDans2{
get { return _Catégories[1]; }
}
public string Différents{
get { return _Catégories[2]; }
}
public string Identiques{
get { return _Catégories[3]; }
}
private string Read(string FileToRead)
{
FileStream OrigFile = new FileStream(FileToRead, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(OrigFile, System.Text.Encoding.Default);
string s = sr.ReadToEnd();
sr.Close();
OrigFile.Close();
return (s);
}
}
}
et le code du programme :
namespace Compare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_Click(object sender, EventArgs e)
{
Comparaison ObjComparaison = new Comparaison(textBox1.Text, textBox2.Text);
textBox3.Text = ObjComparaison.PasDans1;
}
private void radioButton2_Click(object sender, EventArgs e)
{
Comparaison ObjComparaison = new Comparaison(textBox1.Text, textBox2.Text);
textBox3.Text = ObjComparaison.PasDans2;
}
private void radioButton3_Click(object sender, EventArgs e)
{
Comparaison ObjComparaison = new Comparaison(textBox1.Text, textBox2.Text);
textBox3.Text = ObjComparaison.Différents;
}
private void radioButton4_Click(object sender, EventArgs e)
{
Comparaison ObjComparaison = new Comparaison(textBox1.Text, textBox2.Text);
textBox3.Text = ObjComparaison.Identiques;
}
}
}
Ce que je n'arrive pas encore trop à cerner c'est l'utilité d'utiliser une classe.
Peut être préfères tu avoir le projet entier sous les yeux ? Tu peux le télécharger ici :
[ Lien ]En éspèrant ne pas être trop dérangeant,
Mathmax