public static void MoveFolder(string sourcePath, string destPath)
{
if (Directory.Exists(sourcePath))
{
if (!Directory.Exists(destPath))
{
//目標(biāo)目錄不存在則創(chuàng)建
try
{
Directory.CreateDirectory(destPath);
}
catch (Exception ex)
{
throw new Exception("創(chuàng)建目標(biāo)目錄失?。?/span>" + ex.Message);
}
}
//獲得源文件下所有文件
List<string> files = new List<string>(Directory.GetFiles(sourcePath));
files.ForEach(c =>
{
string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
//覆蓋模式
if (File.Exists(destFile))
{
File.Delete(destFile);
}
File.Move(c, destFile);
});
//獲得源文件下所有目錄文件
List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
folders.ForEach(c =>
{
string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
//Directory.Move必須要在同一個(gè)根目錄下移動才有效,不能在不同卷中移動。
//Directory.Move(c, destDir);
//采用遞歸的方法實(shí)現(xiàn)
MoveFolder(c, destDir);
});
}
else
{