Bonjour à tous,

J'ai une application permettant de récupérer des dossiers et des clefs de registres sur des machines connectées au réseau.

Dans un premier temps, je ping les machines afin de savoir si celles ci sont connectées et j'affiche le résultat dans une DGV (1e colonne : nom de la machine, 2e colonne : résultat du ping (oui/non)).

Lorsque la réponse du ping est positive et lorsque j'appuie sur mon bouton "récupération", je récupère sur les machines concernées les dossiers qui vont bien avec un Backgroundworker.

Cependant, lorsque j'ai une erreur sur la copie d'un dossier (par exemple le fichier source n'existe pas, je récupère et traite l’exception), je m'attend à ce que la tâche suivante s’exécute mais ce n'est pas le cas, je rentre directement dans mon Runworkercompleted...

Ci dessous mon code : (pas complet car trop long, j'ai supprimé ce que je ne jugeais pas essentiel, hésitez pas à me demandez plus d'infos)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Net.NetworkInformation;
using Microsoft.Win32;
 
 
namespace OutilMigration
{
 
 
    public partial class Form_Main : Form
    {
                    // ----- VARIABLES GLOBALES ----- //
 
        private int compteurmc = 0;
        private string TempPath = Environment.GetEnvironmentVariable("tmp");
        private string CurrentPath = Directory.GetCurrentDirectory();
        ArrayList lmc = new ArrayList();
 
        //Fonction copie des dossiers
        private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            try
            {
                //Get the subdirectories for the specified directory.
                DirectoryInfo dir = new DirectoryInfo(sourceDirName);
                DirectoryInfo[] dirs = dir.GetDirectories();
              if (!Directory.Exists(sourceDirName))
              {
                  MessageBox.Show("chemin non trouvé");
              }
 
                // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }
 
            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
 
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                //Attribut lecture seule mise à false pour tout les fichiers copiés
                file.IsReadOnly = false;
                file.CopyTo(temppath, false);
            }
 
            // If copying subdirectories, copy them and their contents to new location.
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            } 
            }
            catch (DirectoryNotFoundException dnfe)
            {
                MessageBox.Show(dnfe.Message.ToString(), "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch ( FileNotFoundException fnfe)
            {
                MessageBox.Show(fnfe.Message.ToString());
            }                     
        }
 
        //*******************************************************************************************************//
        //Gestion de la récupération du des fichiers et des registres avec BackgroundWorker
        //*******************************************************************************************************//
 
         //Création d'un backgroundworker pour récuperation fichiers et registres
        private void StartProcessCopie(int nbmachinesel)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = false;
            worker.DoWork += ProcessAsyncCopie;
            worker.RunWorkerCompleted += ResultProcessCopie;
            worker.RunWorkerAsync(nbmachinesel);
        }
 
        //Fonction de recupération des fichiers et registres
        void ProcessAsyncCopie(object sender, DoWorkEventArgs e)
        {            
            for (int i = 0; i <= (int)e.Argument; i++)
            {
                    switch (lmc[i].ToString().Substring(4))
                    {                            
                        case "BC":
                            if (Directory.Exists(TempPath + "\\SYSTEME\\" + lmc[i].ToString()))
                            {
                                Directory.Delete(TempPath + "\\SYSTEME\\" + lmc[i].ToString(), true);
                            }
                            DirectoryCopy("\\\\" + lmc[i].ToString() + "\\SYSTEME\\bdd", TempPath + "\\SYSTEME\\" + lmc[i].ToString() + "\\bdd", true);
                            DirectoryCopy("\\\\" + lmc[i].ToString() + "\\SYSTEME\\son", TempPath + "\\SYSTEME\\" + lmc[i].ToString() + "\\son", true);
                            DirectoryCopy("\\\\" + lmc[i].ToString() + "\\SYSTEME\\symbole", TempPath + "\\SYSTEME\\" + lmc[i].ToString() + "\\symbole", true);                     
                           break;      
                        case "BC2":
                            if (Directory.Exists(TempPath + "\\SYSTEME\\" + lmc[i].ToString()))
                            {
                                Directory.Delete(TempPath + "\\SYSTEME\\" + lmc[i].ToString(), true);
                            } 
                            DirectoryCopy("\\\\" + lmc[i].ToString() + "\\SYSTEME\\son", TempPath + "\\SYSTEME\\" + lmc[i].ToString() + "\\son", true);
                            DirectoryCopy("\\\\" + lmc[i].ToString() + "\\SYSTEME\\symbole", TempPath + "\\SYSTEME\\" + lmc[i].ToString() + "\\symbole", true);   
                            break;                      
                        default:
                            MessageBox.Show("Pas de récupération");
                            break;
                    }
                }
            }
 
        void ResultProcessCopie(object sender, RunWorkerCompletedEventArgs e)
        {           
                MessageBox.Show("Récupération terminée", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
 
        private void btn_migration_Click(object sender, EventArgs e)
       {
        StartProcessCopie(compteurmc);
       }
Avec ce code, lorsque pour le cas "BC", je lui demande de faire une copie du répertoire bdd et que celui ci n'existe pas, au lieu de me mettre un message d'erreur et de continuer pour effectuer la copie du répertoire son, j'ai uniquement le message d'erreur puis directement après le message "Récupération terminée" de mon ResultProcessCopie...

Je ne sais pas si cela est très clair mais j'ai beau chercher, je ne vois pas d'où viens l'erreur (à noter que je n'ai pas de formation en code donc l'erreur peut être toute bête...)

D'avance merci à tous !