C# 复制文件夹,移动文件夹
public static void CopyDir(string fromDir, string toDir)
{
if (!Directory.Exists(fromDir))
return;
if (!Directory.Exists(toDir))
{
Directory.CreateDirectory(toDir);
}
string[] files = Directory.GetFiles(fromDir);
foreach (string formFileName in files)
{
string fileName = Path.GetFileName(formFileName);
string toFileName = Path.Combine(toDir, fileName);
File.Copy(formFileName, toFileName);
}
string[] fromDirs = Directory.GetDirectories(fromDir);
foreach (string fromDirName in fromDirs)
{
string dirName = Path.GetFileName(fromDirName);
string toDirName = Path.Combine(toDir, dirName);
CopyDir(fromDirName, toDirName);
}
}
public static void MoveDir(string fromDir, string toDir)
{
if (!Directory.Exists(fromDir))
return;
CopyDir(fromDir, toDir);
Directory.Delete(fromDir, true);
}
{
if (!Directory.Exists(fromDir))
return;
if (!Directory.Exists(toDir))
{
Directory.CreateDirectory(toDir);
}
string[] files = Directory.GetFiles(fromDir);
foreach (string formFileName in files)
{
string fileName = Path.GetFileName(formFileName);
string toFileName = Path.Combine(toDir, fileName);
File.Copy(formFileName, toFileName);
}
string[] fromDirs = Directory.GetDirectories(fromDir);
foreach (string fromDirName in fromDirs)
{
string dirName = Path.GetFileName(fromDirName);
string toDirName = Path.Combine(toDir, dirName);
CopyDir(fromDirName, toDirName);
}
}
public static void MoveDir(string fromDir, string toDir)
{
if (!Directory.Exists(fromDir))
return;
CopyDir(fromDir, toDir);
Directory.Delete(fromDir, true);
}