Exemple #1
0
        /// <summary>
        /// 批量剪切文件到新的文件夹,忽略中断错误
        /// </summary>
        /// <param name="files">批量文件</param>
        /// <param name="targetPath">目标路径</param>
        /// <param name="consolidateExceptions">是否合并异常,失败不中断</param>
        /// <returns>剪切后的文件</returns>
        /// <example>
        ///     <code>
        ///         var files = directory.GetFiles("*.txt", "*.xml");
        ///         files.MoveTo(@"c:\temp\");
        ///     </code>
        /// </example>
        public static FileInfo[] MoveTo(this FileInfo[] files, string targetPath, bool consolidateExceptions = true)
        {
            List <Exception> exceptions = null;

            foreach (var file in files)
            {
                try
                {
                    var fileName = Path.Combine(targetPath, file.Name);
                    file.MoveTo(fileName);
                }
                catch (Exception e)
                {
                    if (consolidateExceptions)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            if ((exceptions != null) && (exceptions.Count > 0))
            {
                throw JceException.Combine(
                          "Error while copying one or several files, see InnerExceptions array for details.",
                          exceptions.ToArray());
            }
            return(files);
        }
Exemple #2
0
 /// <summary>
 /// 批量删除文件,忽略中断错误
 /// </summary>
 /// <param name="files">批量文件</param>
 /// <param name="consolidateExceptions">是否合并异常,失败不中断</param>
 /// <example>
 ///     <code>
 ///         var files = directory.GetFiles("*.txt", "*.xml");
 ///         files.Delete()
 ///     </code>
 /// </example>
 public static void Delete(this FileInfo[] files, bool consolidateExceptions = true)
 {
     if (consolidateExceptions)
     {
         List <Exception> exceptions = new List <Exception>();
         foreach (var file in files)
         {
             try
             {
                 file.Delete();
             }
             catch (Exception e)
             {
                 exceptions.Add(e);
             }
         }
         if (exceptions.Any())
         {
             throw JceException.Combine(
                       "Error while deleting one or several files, see InnerExceptions array for details.", exceptions);
         }
     }
     else
     {
         foreach (var file in files)
         {
             file.Delete();
         }
     }
 }