using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Install.Core.Common
{
public static class CopyExt
{
public static Exception Error;
public static bool Copy(string sourceDirName, string destDirName)
{
if (!Directory.Exists(sourceDirName))
{
return false;
}
if (!Directory.Exists(destDirName))
Directory.CreateDirectory(destDirName);
string[] files = Directory.GetFiles(sourceDirName);
foreach (string formFileName in files)
{
string fileName = Path.GetFileName(formFileName);
string toFileName = Path.Combine(destDirName, fileName);
try
{
File.Copy(formFileName, toFileName, true);
}
catch (Exception e)
{
Error = e;
return false;
}
}
string[] fromDirs = Directory.GetDirectories(sourceDirName);
foreach (string fromDirName in fromDirs)
{
string dirName = Path.GetFileName(fromDirName);
string toDirName = Path.Combine(destDirName, dirName);
if (!Copy(fromDirName, toDirName))
return false;
}
return true;
}
public static bool CopyFileOrDirectory(string src, string dest)
{
if (File.Exists(src))
{
//文件复制
try
{
File.Copy(src, dest, true);
}
catch (Exception e)
{
Error = e;
return false;
}
return true;
}
else
{
return Copy(src, dest);
}
}
///
/// 从 文件夹 sourceDirName 内 把扩展名为 extname 的文件 复制到 destDirName文件夹内
///
///
///
///
public static bool Copy(string sourceDirName, string destDirName, string[] extnames)
{
if (!Directory.Exists(sourceDirName))
return false;
if (!Directory.Exists(destDirName))
Directory.CreateDirectory(destDirName);
string[] files = Directory.GetFiles(sourceDirName);
foreach (string fromFileName in files)
{
if (extnames.Any(ext => fromFileName.EndsWith(ext)))
{
string fileName = Path.GetFileName(fromFileName);
string toFileName = Path.Combine(destDirName, fileName);
try
{
File.Copy(fromFileName, toFileName, true);
}
catch (Exception e)
{
Error = e;
return false;
}
}
}
return true;
}
}
}