/// <summary> /// Метод, срабатывающий при нажатии на кнопку запуска очистки. /// </summary> private void CM_Clean_Click(object sender, EventArgs e) { if (CM_FTable.Items.Count > 0) { if (CM_FTable.CheckedItems.Count > 0) { if (MessageBox.Show(String.Format(AppStrings.PS_CleanupExecuteQ, CleanInfo), Properties.Resources.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { // Отключаем кнопку отмены, очистки и меняем её текст... CM_Clean.Text = AppStrings.PS_CleanInProgress; CM_Clean.Enabled = false; CM_Clean.Visible = false; // Переключаем видимость контролов... CM_Cancel.Enabled = false; CM_Cancel.Visible = false; PrbMain.Visible = true; // Запускаем поток для выполнения очистки... if (!ClnWrk.IsBusy) { ClnWrk.RunWorkerAsync(); } } } else { MessageBox.Show(AppStrings.PS_SelectItemsMsg, Properties.Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show(AppStrings.PS_LoadErr, Properties.Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// "Execute cleanup" button click event handler. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void CM_Clean_Click(object sender, EventArgs e) { if (CM_FTable.Items.Count > 0) { if (CM_FTable.CheckedItems.Count > 0) { if (MessageBox.Show(String.Format(AppStrings.PS_CleanupExecuteQ, CleanInfo), Properties.Resources.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { ChangeControlsState(); if (!ClnWrk.IsBusy) { ClnWrk.RunWorkerAsync(GetDeleteFilesList()); } } } else { MessageBox.Show(AppStrings.PS_SelectItemsMsg, Properties.Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show(AppStrings.PS_LoadErr, Properties.Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// Метод, срабатывающий асинхронно при запуске механизма очистки. /// </summary> private void ClnWrk_DoWork(object sender, DoWorkEventArgs e) { try { // Задаём массив для хранения имён удаляемых файлов... List <string> DeleteQueue = new List <string>(); // Добавляем в очередь для очистки... Invoke((MethodInvoker) delegate() { CM_Info.Text = AppStrings.PS_ProcessPrepare; foreach (ListViewItem LVI in CM_FTable.Items) { if (LVI.Checked) { DeleteQueue.Add(LVI.ToolTipText); } } }); // Добавляем в архив (если выбрано)... if (Properties.Settings.Default.PackBeforeCleanup || ForceBackUp) { Invoke((MethodInvoker) delegate() { CM_Info.Text = AppStrings.PS_ProgressArchive; }); if (!FileManager.CompressFiles(DeleteQueue, FileManager.GenerateBackUpFileName(FullBackUpDirPath, Properties.Resources.BU_PrefixDef))) { MessageBox.Show(AppStrings.PS_ArchFailed, Properties.Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } // Меняем текст в строке статуса... Invoke((MethodInvoker) delegate() { CM_Info.Text = AppStrings.PS_ProgressCleanup; }); // Формируем счётчики... int TotalFiles = DeleteQueue.Count; int i = 1, j = 0; // Удаляем файлы из очереди очистки... foreach (string Fl in DeleteQueue) { try { j = (int)Math.Round(((double)i / (double)TotalFiles * (double)100.00), 0); i++; if ((j >= 0) && (j <= 100)) { ClnWrk.ReportProgress(j); } } catch (Exception Ex) { CoreLib.WriteStringToLog(Ex.Message); } try { if (File.Exists(Fl)) { File.SetAttributes(Fl, FileAttributes.Normal); File.Delete(Fl); } } catch (Exception Ex) { CoreLib.WriteStringToLog(Ex.Message); } } // Удалим пустые каталоги (если разрешено)... if (Properties.Settings.Default.RemoveEmptyDirs) { try { foreach (string Dir in CleanDirs) { FileManager.RemoveEmptyDirectories(Path.GetDirectoryName(Dir)); } } catch (Exception Ex) { CoreLib.HandleExceptionEx(AppStrings.PS_CleanEmptyDirsError, Properties.Resources.AppName, Ex.Message, Ex.Source, MessageBoxIcon.Warning); } } } catch (Exception Ex) { // Произошло исключение... CoreLib.HandleExceptionEx(AppStrings.PS_CleanupErr, Properties.Resources.AppName, Ex.Message, Ex.Source, MessageBoxIcon.Warning); } }
/// <summary> /// Removes all selected files and directories. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Additional arguments.</param> private void ClnWrk_DoWork(object sender, DoWorkEventArgs e) { // Extracting list from arguments... List <String> DeleteQueue = e.Argument as List <String>; // Creating backup if enabled or required by policy... if (Properties.Settings.Default.PackBeforeCleanup || ForceBackUp) { ClnWrk.ReportProgress(0, AppStrings.PS_ProgressArchive); if (!FileManager.CompressFiles(DeleteQueue, FileManager.GenerateBackUpFileName(FullBackUpDirPath, Properties.Resources.BU_PrefixDef))) { Logger.Error(AppStrings.PS_ArchFailed); } } // Reporting new status... ClnWrk.ReportProgress(0, AppStrings.PS_ProgressCleanup); // Creating some counters... int TotalFiles = DeleteQueue.Count; int CurrentFile = 1, CurrentPercent; // Removing all files from list... foreach (string Fl in DeleteQueue) { try { // Removing file if exists... if (File.Exists(Fl)) { File.SetAttributes(Fl, FileAttributes.Normal); File.Delete(Fl); } // Reporting progress to form... CurrentPercent = (int)Math.Round(CurrentFile / (double)TotalFiles * 100.00d, 0); CurrentFile++; if ((CurrentPercent >= 0) && (CurrentPercent <= 100)) { ClnWrk.ReportProgress(CurrentPercent); } } catch (Exception Ex) { Logger.Warn(Ex); } } // Removing empty directories if allowed... if (Properties.Settings.Default.RemoveEmptyDirs) { foreach (string Dir in CleanDirs) { try { FileManager.RemoveEmptyDirectories(Path.GetDirectoryName(Dir)); } catch (Exception Ex) { Logger.Error(Ex, DebugStrings.AppDbgExClnEmptyDirs); } } } }