File.Copy不会复制所有文件夹和目录 [英] File.Copy do not copy all the folder and directories
问题描述
大家好,我调用file.copy将文件和文件夹从源文件复制到目标文件。当我在没有调试器的情况下运行它时,File.copy没有将所有内容复制到目标。当我使用调试器调试程序时,代码就像魅力一样,任何人都知道为什么file.copy不会在没有调试器的情况下复制所有内容?以下是代码:
Hi guys,i called file.copy to copy file and folder from source to destination. When i run it without debugger the File.copy did not copy everything to destination. When I debug the program with debugger the code works like a charm, anyone have any idea why the file.copy do not copy everything without debugger? Below is the code:
public bool CopyFileAndFolder(string sourceFolder, string replacePath)
{
bool result = false;
try
{
foreach (string extractPath in Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories))
{
string destFolder = extractPath.Replace(sourceFolder, replacePath);
if (!Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
}
foreach (string extractFile in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
{
string destFile = extractFile.Replace(sourceFolder, replacePath);
File.Copy(extractFile, destFile, true);
}
result = true;
}
catch (Exception ex)
{
result = false;
Trace.WriteLine(ex.Message);
}
return result;
}
推荐答案
您可以尝试更改这样的代码
You could try to change the code like this
public bool CopyFileAndFolder(string sourceFolder, string replacePath)
{
bool result = false;
try
{
foreach (string extractPath in Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories))
{
string destFolder = extractPath.Replace(sourceFolder, replacePath);
if (!Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
foreach (string extractFile in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
{
string destFile = Path.Combine(destFolder, Path.GetFileName(extractFile));
File.Copy(extractFile, destFile, true);
}
}
result = true;
}
catch (Exception ex)
{
result = false;
Trace.WriteLine(ex.Message);
}
return result;
}
也就是说,将文件复制循环移动到目录搜索循环中并稍微改变目标路径。
[更新]
我发现更容易使这个方法递归。据我所知,它对我有用。
That is, move the file copy loop into the directory search loop and change the destination path a bit.
[UPDATE]
I found it easier to make this method recursive instead. As far as I can tell it works for me.
// Recursive version
public bool CopyFileAndFolder(string sourceFolder, string backupPath)
{
bool retVal = false;
try
{
// First create the new backup directory
if (!Directory.Exists(backupPath))
Directory.CreateDirectory(backupPath);
// Then copy all the files in this directory
foreach (string fileName in Directory.GetFiles(sourceFolder, "*.*", SearchOption.TopDirectoryOnly))
{
string destFile = Path.Combine(backupPath, Path.GetFileName(fileName));
File.Copy(fileName, destFile, true);
}
// Get the sub-diretories
string[] directories = Directory.GetDirectories(sourceFolder, "*", SearchOption.TopDirectoryOnly);
// This check is just to make the recursion break point more clear
if (directories.Length == 0)
return true;
foreach (string directory in directories)
{
string destFolder = Path.Combine(backupPath, Path.GetFileName(directory));
retVal = CopyFileAndFolder(directory, destFolder);
if (!retVal)
break;
}
}
catch (Exception ex)
{
retVal = false;
Trace.WriteLine(ex.Message);
}
return retVal;
}
这篇关于File.Copy不会复制所有文件夹和目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!