.NET 2.x Общее копирование файлов backgroundWorker1 - C#
Формулировка задачи:
Вот та код
Вывести прогресс скопированных файлов
Не работает пишет backgroundWorker1 занят
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
for (int i=0; i<10; i++)
{
Copy_Dowork(@"C:\Video_"+i+".avi",@"D:\Video_"+i+".avi");
}
}
private void CopyFiles(string file_old, string file_new)
{
File.Copy(file_old,file_new,true);
}
public void Copy_Dowork(string old_f,string new_f)
{
old_put = old_f;
new_put = new_f;
}
public string old_put { get; set; }
public string new_put { get; set; }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
CopyFiles(old_put,new_put);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = "Прогресс: " + e.ProgressPercentage + " %";
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
MessageBox.Show("ОТМЕНА!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (e.Error != null)
{
MessageBox.Show(e.Error.Message, "ОШИБКА", MessageBoxButtons.OK, MessageBoxIcon.Error);
//resultLabel.Text = "Error: " + e.Error.Message;
}
/*else
{
MessageBox.Show("Упаковано", "ОШИБКА", MessageBoxButtons.OK, MessageBoxIcon.Information);
//resultLabel.Text = "Done!";
}*/
}Решение задачи: «.NET 2.x Общее копирование файлов backgroundWorker1»
textual
Листинг программы
public void CreateParts(string pathBaseFolder, string pathParts, string name_part, int partCount, string copy_move)
{
long files_all_size = 0;
long folder_size_part = 0;
string dir_file_path = "";
long size_files_all = 0; // весь размер файлов
int size_value = 0;
//progressBar1.Minimum = 0;
//progressBar1.Maximum = N_brower_files;
//progressBar1.Step = 1;
//long Mbyte_files_all_size = 0;
//long Mbyte_folder_size_part = 0;
files = new List<FileItem>();
//progressBar1.Maximum = size_files_all;
//backgroundWorker1.ReportProgress(size_files_all);
string[] folderName = new string[partCount]; //стрковый масив, число в строки разбил
FileSearch(pathBaseFolder);// размещаем в files все файлы из каталога
files = files.OrderByDescending(f => f.Size).ToList(); // сортируем эти файлы по размеру
for (int i = 0; i < files.Count; i++)
{
size_files_all += files[i].Size; // Когда отсортированы по размеру , файлы добавляються в List
// Тогда можно получить общий размер
}
progressBar1.Maximum = (int)size_files_all;
// условие если папки нету part то создавать
if (!Directory.Exists(pathParts)) Directory.CreateDirectory(pathParts);
for (int i = 0; i < partCount; i++)// создаем папки 5
{
// записываем строковый масив part1 part2......5
folderName[i] = pathParts + @"\" + name_part + (i + 1).ToString();
//folderName[i] = RemoveSpaces(folderName[i]);
// создаем папки
Directory.CreateDirectory(pathParts + @"\" + name_part + (i + 1).ToString());
}
var folders = new long[partCount];
// далее, попробуй составить алгоритм разбиения списка файлов на части с сохранением в part1...5
switch (copy_move)
{
case mass_cop:
// сортируем по размеру
foreach (var f in files.OrderByDescending(f => f.Size))
{
var i = FindMinFolder(folders);
f.TargetFolder = i;
folders[i] += f.Size;
size_value += (int)f.Size;
try
{
dir_file_path = f.Dir.Replace(pathBaseFolder, "");
if (String.IsNullOrEmpty(folderName[i]))
{
Directory.CreateDirectory(pathParts + @"\" + name_part + "1" + dir_file_path);
//Copy_Files_ProgressBar(f.Path,pathParts + @"\" + name_part + "1" + dir_file_path + @"\" +Path.GetFileName(f.Path));
//backgroundWorker1.RunWorkerAsync();
Copy_progress(f.Path, pathParts + @"\" + name_part + "1" + dir_file_path + @"\" + Path.GetFileName(f.Path));
progressBar1.Value = size_value ;
//MessageBox.Show(size_value + "\n" + (int)size_files_all + "\n\nBAR\n" + progressBar1.Value + "\n" + progressBar1.Maximum);
/////////progressBar1//////////
//label3.Text = "Прогресс: " + (folders[i] * 100) / size_files_all + " %";
//progressBar1.Value = (int)folders[i] / (int)size_files_all;
//MessageBox.Show(progressBar1.Value + "\n" + progressBar1.Maximum + "\n" + (folders[i] * 100) / size_files_all + "%");
/*if (File.Exists(pathParts + @"\" + name_part + "1" + dir_file_path + @"\" + Path.GetFileName(f.Path)))
{
progressBar1.PerformStep();
}*/
/////////progressBar1//////////
}
else
{
Directory.CreateDirectory(folderName[i] + @"\" + dir_file_path);
//Copy_Files_ProgressBar(f.Path, folderName[i] + dir_file_path + @"\" + Path.GetFileName(f.Path));
//backgroundWorker1.RunWorkerAsync();
Copy_progress(f.Path, folderName[i] + dir_file_path + @"\" + Path.GetFileName(f.Path));
/////////progressBar1//////////
progressBar1.Value = size_value;
//MessageBox.Show(size_value + "\n" + (int)size_files_all + "\n\nBAR\n" + progressBar1.Value + "\n" + progressBar1.Maximum);
//MessageBox.Show(size_value + "\n" + size_files_all);
//progressBar1.Value = 50;
//label3.Text = "Прогресс: " + (folders[i] * 100) / size_files_all + " %";
//progressBar1.Value = (int)folders[i] / (int)size_files_all;
//MessageBox.Show(progressBar1.Value + "\n" + progressBar1.Maximum + "\n" + (folders[i] * 100) / size_files_all + "%");
/*if (File.Exists(folderName[i] + dir_file_path + @"\" + Path.GetFileName(f.Path)))
{
progressBar1.PerformStep();
}*/
/////////progressBar1//////////
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ОШИБКА", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//if (progressBar1.Value == progressBar1.Maximum)
//{
MessageBox.Show("Распределение закончино!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Value = 0;
//}
break;
case mass_mov:
break;
default:
MessageBox.Show("Не верно: " + copy_move, "ОШИБКА", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}