我可以复制多个目录中的所有文件,但我想要做的是将所有目录与其中的文件复制,因为它们是我从哪里复制而不是把文件放在我的目标文件夹中。这是到目前为止我的代码我已经得到了MSDN代码,但仍然无法让我的程序与它一起工作
{
string SelectedPath = (string)e.Argument;
string sourceDirName;
string destDirName;
bool copySubDirs;
DirectoryCopy(".", SelectedPath, true);
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory does not exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
// Create the path to the new copy of the file.
string temppath = Path.Combine(destDirName, file.Name);
// Copy the file.
file.CopyTo(temppath, false);
}
// If copySubDirs is true, copy the subdirectories.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string temppath = Path.Combine(destDirName, subdir.Name);
// Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
任何帮助将不胜感激
叶我已经看清楚了尝试,但它并没有在我的工作代码由于某种原因 – bobthemac 2011-02-04 17:15:38
是的,原因是什么?它必须给出一个错误。 – Karl 2011-02-04 17:17:45