Exemplo n.º 1
0
        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="dstPath"></param>
        /// <param name="NeedCopy"></param>
        public static void CopyFolder(string srcPath, string dstPath, NeedCopyFile NeedCopy = null)
        {
            if (Directory.Exists(dstPath))
            {
                Directory.Delete(dstPath);
            }
            if (File.Exists(dstPath))
            {
                File.Delete(dstPath);
            }

            Directory.CreateDirectory(dstPath);

            foreach (var file in Directory.GetFiles(srcPath))
            {
                if (NeedCopy == null || (NeedCopy != null && NeedCopy(Path.GetFileName(file))))
                {
                    File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)));
                }
            }

            foreach (var dir in Directory.GetDirectories(srcPath))
            {
                CopyFolder(dir, Path.Combine(dstPath, Path.GetFileName(dir)), NeedCopy);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 拷贝文件
 /// </summary>
 /// <returns><c>true</c>, if file was copyed, <c>false</c> otherwise.</returns>
 /// <param name="sourcePath">Source path.</param>
 /// <param name="targetPath">Target path.</param>
 public static bool CopyFile(string sourcePath, string targetPath, NeedCopyFile NeedCopy = null)
 {
     if (File.Exists(sourcePath))
     {
         if (File.Exists(targetPath))
         {
             File.Copy(sourcePath, targetPath, true);
         }
         else if (NeedCopy == null || (NeedCopy != null && NeedCopy(Path.GetFileName(sourcePath))))
         {
             File.Copy(sourcePath, targetPath);
         }
         return(true);
     }
     else
     {
         Debug.LogError("要拷贝的文件为空:" + sourcePath);
         return(false);
     }
 }