IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[C#] - dossiers avec beaucoup de fichiers


Sujet :

Windows Forms

  1. #1
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Juin 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 5
    Points : 4
    Points
    4
    Par défaut [C#] - dossiers avec beaucoup de fichiers
    Bonjour à tous,

    Voici mon soucis, j'ai un petit programme qui scanne le contenu d'un dossier, qui détecte les doublons et qui restitue les fichiers sans doublons dans un nouveau dossier. Le code fonctionne avec un petit nombre d'éléments mais plante si le nombre d'éléments devient trop grand, auriez-vous une idée de pourquoi?

    Voici le code complet:
    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
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
     
     
    using System;
    using System.IO;
    using System.Globalization;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
     
            public List<string> fichiers = new List<string>();
            public Form1()
            {
                InitializeComponent();
                label4.Text = "";
            }
     
            private void button3_Click(object sender, EventArgs e)
            {
                progressBar1.Maximum = 100;
                progressBar1.Minimum = 0;
                progressBar1.Step = 1;
                label4.Text = "";
                //DirectoryInfo directoryInfo1 = new DirectoryInfo(textBox1.Text);     
                //FileInfo[] files = directoryInfo1.GetFiles();
     
                //int test = files.Count();
     
                //if (test != 0)
                //{
     
                progressBar1.Cursor.Equals(50);
     
                //}           
                bool exists = System.IO.Directory.Exists(textBox1.Text);
                if (!exists)
                {
                    MessageBox.Show("Le répertoire de destination n'existe pas");
                }
                else {
                bool exists2 = System.IO.Directory.Exists(textBox2.Text);
                if (!exists2)
                {
                    MessageBox.Show("Le répertoire à analyser n'existe pas");
                }
                else
                {
     
                    //DirectoryInfo directoryInfo1 = new DirectoryInfo(textBox1.Text);
                    //FileInfo[] files = directoryInfo1.GetFiles();
                    //FileInfo[] files = directoryInfo1.GetFiles();
     
                    if (Directory.EnumerateFileSystemEntries(textBox1.Text).Any())
                    {
                        MessageBox.Show("Le répertoire de destination doit être vide!");
     
                    }
                    else
                    {
     
                        if (textBox1.Text != "" && textBox2.Text != "")
                        {
                            fichiers.Clear();
                            lancerAnalyse(textBox2.Text);
                            contenuTableau();
                            createFiles();
                            label4.Text = "L'analyse est terminée.";
                            MessageBox.Show("L'analyse est terminée.");
     
                        }
                        else
                        {
                            MessageBox.Show("Vous n'avez pas indiqué les chemins pour l'opérations");
     
                        }
                    }
                }
                }
     
     
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                var FolderBrowserDialog1 = new FolderBrowserDialog();
                if (FolderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    textBox2.Text = FolderBrowserDialog1.SelectedPath;
     
                }
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                var FolderBrowserDialog1 = new FolderBrowserDialog();
                if (FolderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    textBox1.Text = FolderBrowserDialog1.SelectedPath;
     
                }
            }
     
            public void lancerAnalyse(string Chemin1)
            {
     
     
                DirectoryInfo directoryInfo1 = new DirectoryInfo(Chemin1);
     
                label4.Text = Chemin1;
                Application.DoEvents();
     
                FileInfo[] files = directoryInfo1.GetFiles();
                int y = files.Count();
                int i = 0;
                foreach (FileInfo file in files)
                {
     
                    i++;
     
                    fichiers.Add(file.FullName);
     
     
                    if (y != 0)
                    {
                        decimal a = i;
                        decimal b = y;
                        decimal value = Convert.ToDecimal(a / b * 100);
                        int value2 = Convert.ToInt32(value);
     
     
                        progressBar1.Value = value2;
                    }
     
                //int file1byte;
                //int file2byte;
                //FileStream fs1;
                //FileStream fs2;
     
                //do
                //{
                //    // Read one byte from each file.
                //    file1byte = fs1.ReadByte();
                //    file2byte = fs2.ReadByte();
                //}
                //while ((file1byte == file2byte) && (file1byte != -1));
     
                }
     
                //Recursivite, la fonction s'appelle elle même pour faire la même ôpération dans les sous-dossiers
                DirectoryInfo[] dirs = directoryInfo1.GetDirectories();
                foreach (DirectoryInfo dir in dirs)
                {
                    lancerAnalyse(dir.FullName);
     
                }
     
     
            }
     
            public void contenuTableau()
            {
                //List<int> listTEMP = new List<int>();
     
                for (int i = 0; i<fichiers.Count; i++ )
                {
     
                    for (int t = i+1; t<fichiers.Count; t++){
     
     
                        string testi = fichiers[i];
                        string testt = fichiers[t];
     
                        if (fichiers[t] != "0" && fichiers[i] != "0")
                        {
                            if (FileCompare(fichiers[i], fichiers[t]))
                            {
     
     
                                fichiers[t] = "0";
     
     
                            }
                        }
     
     
     
     
                        //int file1byte;
                        //int file2byte;
                        //MemoryStream stream = new MemoryStream();
                        //MemoryStream stream2 = new MemoryStream();
                        //StreamWriter writer2 = new StreamWriter(stream2);
                        //StreamWriter writer = new StreamWriter(stream);
                        //writer.Write(fichiers[t]);
                        //writer2.Write(fichiers[i]);
     
                        //do
                        //{
     
                        //    //Read one byte from each file.
                        //    file1byte = stream.ReadByte();
                        //    file2byte = stream2.ReadByte();
                        //}
                        //while ((file1byte == file2byte) && (file1byte != -1));
     
                        //if (writer == writer2)
                        //{
                        //    fichiers.RemoveAt(t);
     
                        //}
     
                        //stream.Close();
                        //stream2.Close();
     
                      }
     
                    decimal a = i;
                    decimal b = fichiers.Count;
                    decimal value = Convert.ToDecimal(a/b*100);
                    int value2 = Convert.ToInt32(value);
     
                    label4.Text = "Localisation des doublons";
                    progressBar1.Value = value2;
     
                    }
     
                fichiers.RemoveAll(item => item == "0");
     
                //for (int i = 0; i <= fichiers.Count; i++ )
                //{
                //    if (fichiers[i] == "0")
                //    {
                //        fichiers.RemoveAt(i);
     
                //    }
     
     
     
                //}
     
     
                }
     
     
            public void createFiles()
            {
                progressBar1.Increment(0);
                for (int i = 0; i < fichiers.Count; i++)
                {
     
                    //MessageBox.Show(fichiers[i]);
     
                    string result = Path.GetFileName(fichiers[i]);
                    string nomSansExtension = Path.GetFileNameWithoutExtension(fichiers[i]);
                    string extension = Path.GetExtension(fichiers[i]);
     
                    FileInfo fi = new FileInfo(fichiers[i]);
                    DateTime date = fi.LastAccessTime;
     
                    //MessageBox.Show(date.ToString());
                    //MessageBox.Show(date.ToString("dd-MMMM-yyyy"));
     
                    bool exists = System.IO.Directory.Exists(textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy"));
                    if (!exists)
                    System.IO.Directory.CreateDirectory(textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy"));
     
                    int y = 0;
     
                    bool exists2; 
                    do
                    {
                        y++;
     
                    } while (exists2 = System.IO.File.Exists(textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy") + "\\" + nomSansExtension + " - " + y + extension));
     
     
                    System.IO.File.Copy(fichiers[i], textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy") + "\\" + nomSansExtension + " - " + y + extension);
     
     
     
                    //bool exists = System.IO.Directory.Exists(textBox1.Text + "\\" + date.ToString("f", CultureInfo.CreateSpecificCulture("fr-FR")));
                    //if (!exists)
                    //    System.IO.Directory.CreateDirectory(textBox1.Text + "\\" + date.ToString("f", CultureInfo.CreateSpecificCulture("fr-FR")));
     
     
     
                    //System.IO.File.Copy(fichiers[i], textBox1.Text + "\\" + date.ToString("f", CultureInfo.CreateSpecificCulture("fr-FR")) + "\\" + result);
     
     
                    decimal a = i;
                    decimal b = fichiers.Count;
                    decimal value = Convert.ToDecimal(a / b * 100);
                    int value2 = Convert.ToInt32(value);
     
                    label4.Text = "Ecriture des fichiers dans le dossier de destination";
                    progressBar1.Increment(+value2);
     
                }
     
     
     
     
            }
     
            private bool FileCompare(string file1, string file2)
            {
                int file1byte;
                int file2byte;
                FileStream fs1;
                FileStream fs2;
     
                // Determine if the same file was referenced two times.
                if (file1 == file2)
                {
                    // Return true to indicate that the files are the same.
                    return true;
                }
     
                // Open the two files.
                fs1 = new FileStream(file1, FileMode.Open);
                fs2 = new FileStream(file2, FileMode.Open);
     
                // Check the file sizes. If they are not the same, the files 
                // are not the same.
                if (fs1.Length != fs2.Length)
                {
                    // Close the file
                    fs1.Close();
                    fs2.Close();
     
                    // Return false to indicate files are different
                    return false;
                }
     
                // Read and compare a byte from each file until either a
                // non-matching set of bytes is found or until the end of
                // file1 is reached.
                do
                {
                    // Read one byte from each file.
                    file1byte = fs1.ReadByte();
                    file2byte = fs2.ReadByte();
                }
                while ((file1byte == file2byte) && (file1byte != -1));
     
                // Close the files.
                fs1.Close();
                fs2.Close();
     
                // Return the success of the comparison. "file1byte" is 
                // equal to "file2byte" at this point only if the files are 
                // the same.
                return ((file1byte - file2byte) == 0);
            }
     
            }
     
     
            }
    Merci d'avance!

  2. #2
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Bonjour,
    Quel est le message d'erreur ?

  3. #3
    Membre chevronné
    Avatar de PixelJuice
    Homme Profil pro
    Ingénieur .NET & Game Designer
    Inscrit en
    Janvier 2014
    Messages
    640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Ingénieur .NET & Game Designer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2014
    Messages : 640
    Points : 2 149
    Points
    2 149
    Par défaut
    Bonjour,

    quand tu dis "plante", tu veux dire que le programme "freeze" et que tu ne peux plus rien faire ?

    Car quand je vois une méthode récursive et l'absence de Thread, BackgroundWorker ou Task, je soupçonne que ce soit ça.

    Donc soit :

    - Le traitement est trop long, et vu qu'il est effectué par le thread de l'UI, le programme freeze.
    - Ta méthode fait une boucle infinie dans un cas précis, mais j'en doute vu que tu dis qu'elle fonctionne avec peu d'éléments.

    Mets tout ce qui est récursif dans un Thread ou un BackgroundWorker, et ton problème devrait être résolu.

  4. #4
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Juin 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Merci pour les pistes, je regarde ça et je vous remettrai le code à nouveau.

Discussions similaires

  1. Selection dossier avec visu des fichiers
    Par tAKAmAkA dans le forum IHM
    Réponses: 1
    Dernier message: 30/11/2008, 17h00
  2. Zip d'un dossier avec ou sans fichier
    Par titeZ dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 11/03/2008, 00h45
  3. manipulation des fichiers et dossiers avec VBA excel
    Par GBAGO dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 29/07/2007, 23h30
  4. [VBA-E] créer un dossier avec un fichier résultat
    Par alex.a dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 24/04/2007, 16h43
  5. gestion des fichiers et dossiers avec FTP
    Par ahmeds dans le forum Réseau/Web
    Réponses: 3
    Dernier message: 12/10/2006, 13h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo