我正在尝试将文件夹从一个位置移动到另一位置。如果该文件夹已经存在于目标目录中,但是其中的文件不存在,它将移动文件。我不知道要移动的是子文件夹,如果子文件夹已经存在于目标目录中,那么我需要移动其中的文件。
else if (Directory.Exists(source) && Directory.Exists(target))
{
string[] sourcefiles = Directory.GetFiles(source);
foreach (string sourcefile in sourcefiles)
{
string fileName = Path.GetFileName(sourcefile);
string destFile = Path.Combine(target, fileName);
if (!File.Exists(destFile))
{
File.Move(sourcefile, destFile);
}
DirectoryInfo dir = new DirectoryInfo(source);
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(target, subdir.Name);
if (Directory.Exists(temppath))
{
//maybe another loop here to check if files exist in
subfolders??
}
else
{
Directory.Move(subdir.FullName, temppath);
}
}
}
It's only moving files but not subfolders. I need this to also move subfolders. Thanks!