示例#1
0
 private void RunDeleteOperations(int allocatedPercentage, int percentageProgress, IList <string> deletePaths, bool isFile)
 {
     for (var i = 0; i < deletePaths.Count; i++)
     {
         CheckStopped();
         var deletePath = deletePaths[i];
         OnActionChanged(string.Format("Deleting {0}", deletePath));
         if (isFile && File.Exists(deletePath))
         {
             try
             {
                 _log.InfoFormat("Deleting file {0}", deletePath);
                 File.Delete(deletePath);
             }
             catch (Exception ex1)
             {
                 // Attempt to delete after stripping attibutes
                 _log.WarnFormat("Error deleting file {0}.", deletePath);
                 _log.Warn(ex1.Message, ex1);
                 _log.Warn("Attempting by stripping attributes ...");
                 try
                 {
                     File.SetAttributes(deletePath, FileAttributes.Normal);
                     File.Delete(deletePath);
                 }
                 catch (Exception ex)
                 {
                     var message = string.Format("Error deleting file {0}", deletePath);
                     _log.Error(message);
                     _log.Error(ex.Message, ex);
                     OnErrorOccured(message, ex);
                 }
             }
         }
         else if (!isFile && Directory.Exists(deletePath))
         {
             try
             {
                 _log.InfoFormat("Deleting directory {0}", deletePath);
                 Directory.Delete(deletePath, true);
             }
             catch (Exception ex1)
             {
                 // Attempt to delete one by one
                 _log.WarnFormat("Error deleting directory {0}.", deletePath);
                 _log.Warn(ex1.Message, ex1);
                 _log.Warn("Attempting to delete individually ...");
                 try
                 {
                     Recycler.Delete(new DirectoryInfo(deletePath));
                 }
                 catch (Exception ex)
                 {
                     var message = string.Format("Error deleting directory {0}", deletePath);
                     _log.Error(message);
                     _log.Error(ex.Message, ex);
                     OnErrorOccured(message, ex);
                 }
             }
         }
         OnProgressChanged(percentageProgress + (i + 1) * allocatedPercentage / deletePaths.Count);
     }
 }