我可以複製多個目錄中的所有文件,但我想要做的是將所有目錄與其中的文件複製,因爲它們是我從哪裏複製而不是把文件放在我的目標文件夾中。這是到目前爲止我的代碼我已經得到了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