Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionusing System; using System.ComponentModel; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace RechRecursive { public partial class Form1 : Form { int count = 0; public Form1() { InitializeComponent(); //BackgroundWorker bgw1 = new BackgroundWorker(); backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); pictureBox1.Visible = false; } private void button1_Click(object sender, EventArgs e) { var dlg = new FolderBrowserDialog(); if (dlg.ShowDialog() == DialogResult.OK) { string path = dlg.SelectedPath; textBox1.Text = path; } } private void button2_Click(object sender, EventArgs e) { //pictureBox1.Visible = true; //backgroundWorker1.RunWorkerAsync(); backgroundWorker1.RunWorkerAsync(pictureBox1.Visible=true); ExecRecherche(); } private void ExecRecherche() { //DisplayPleaseWait(); string ds = textBox1.Text; string term = "*" + textBox2.Text + "*"; Recherche(ds, term); } private void Recherche(string ds, string term) { try { string[] fichiers = Directory.GetFiles(ds, term); foreach (string fichier in fichiers) { count++; ListViewItem itm = listView1.Items.Add(count.ToString()); itm.SubItems.Add(fichier); } } catch { } try { string[] dossiers = Directory.GetDirectories(ds); foreach (string dossier in dossiers) { Recherche(dossier, term); } } catch { } } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { ExecRecherche(); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { private List<string> ListFichiers; public Form1() { InitializeComponent(); } /// <summary> /// chargement de la form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { listView1.Items.Clear(); pictureBox1.Visible = false; } /// <summary> /// Sélection d'un répertoire /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBrowserDialog(); if (dlg.ShowDialog() == DialogResult.OK) { string Path = dlg.SelectedPath; label1.Text = Path; } } /// <summary> /// Liste des fichiers /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button2_Click(object sender, EventArgs e) { pictureBox1.Visible = true; pictureBox1.Show(); // le gif animé Cursor = Cursors.WaitCursor; // curseur en attente backgroundWorker1.RunWorkerAsync(); // on demarre la recherche en BackGround } /// <summary> /// REcherche des fichiers en BackGround /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string ds = label1.Text; // dossier de recherche // si le filtre est vide on recherche tous les fichiers string term = textBox2.Text == string.Empty ? "*.*" : "*" + textBox2.Text + "*"; ListFichiers = new List<string>(); Recherche(ds, term); } /// <summary> /// Recherche des fichiers avec récusivité /// </summary> /// <param name="ds"></param> /// <param name="term"></param> private void Recherche(string ds, string term) { try { string[] fichiers = Directory.GetFiles(ds, term); foreach (string fichier in fichiers) { ListFichiers.Add(fichier); } } catch { } try { string[] dossiers = Directory.GetDirectories(ds); foreach (string dossier in dossiers) { Recherche(dossier, term); } } catch { } } } /// <summary> /// fin du BackGround et affichage des fichiers /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Cursor = Cursors.Default; pictureBox1.Visible = false; // on arrête le Gif animé listView1.BeginUpdate(); // on affiche les fichiers dans la listview for (int i = 0; i <= ListFichiers.Count - 1; i++) listView1.Items.Add(new ListViewItem(new string[] { ListFichiers[i] })); listView1.EndUpdate(); } } }
private void button2_Click(object sender, EventArgs e) { //pictureBox1.Visible = true; //backgroundWorker1.RunWorkerAsync(); backgroundWorker1.RunWorkerAsync(pictureBox1.Visible=true); ExecRecherche(); }
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { int count = 0; foreach(string fichier in resultats) { count++; ListViewItem itm = listView1.Items.Add(count.ToString()); itm.SubItems.Add(fichier); } }
Modifié le 17 juil. 2021 à 23:03
Tu me parles chinois pour l'instant, XD
En tout cas, merci de ta réponse.
Je vais voir ce que je peux faire.
J'ai essayé : pour l'instant, ça foire. ^^
++