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
|
using System;
using System.IO;
using System.Linq;
using System.ComponentModel;
public class Form1
{
private string m_TempFolder;
public Form1()
{
// Cet appel est requis par le concepteur.
InitializeComponent();
// Ajoutez une initialisation quelconque après l'appel InitializeComponent().
BackgroundWorker1.WorkerReportsProgress = true;
m_TempFolder = @"D:\TempFolder";
}
private void Button1_Click(object sender, EventArgs e)
{
if (!BackgroundWorker1.IsBusy)
BackgroundWorker1.RunWorkerAsync();
}
private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
string[] Unique_Files = File.ReadAllLines(@"D:\CopyPasteFile.txt");
// '''Partant du principe que le contenu de ton fichier .txt ressemble à ceci (des chemins de fichiers existants) :
// D:\10_Famille.txt
// D:\20190331202606_aclExtraction.txt
// D:\test.txt
// D:\txt.txt
// '''On parcours le tableau de string :
int i = 0;
foreach (string line in Unique_Files)
{
i += 1;
// 'On mets à jour la progressbar et le label
BackgroundWorker1.ReportProgress((i / Unique_Files.Count()) * 100, line);
// 'On copie le fichier en cours
File.Copy(line, Path.Combine(m_TempFolder, new FileInfo(line).Name), true);
}
// 'On indique à la routine de completed que le job est terminé
e.Result = new string[2] { "Success", i.ToString() + " fichier(s) copié(s)." };
}
catch (Exception ex)
{
// 'On indique à la routine de completed que le job a rencontré une erreur
e.Result = new string[2] { "Error", ex.ToString() };
}
}
private void BackgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Value = e.ProgressPercentage;
Label1.Text = "Copie du fichier : " + e.UserState.ToString();
}
private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
}
else
switch (e.Result(0).ToString())
{
case "Success":
{
break;
}
case "Error":
{
break;
}
}
ProgressBar1.Value = 0;
Label1.Text = "";
}
} |
Partager