如何将一个目录中的所有内容复制到另一个目录中,并将每个文件循环出去?复制目录中的所有文件
回答
你不行。但你可以使用某种简洁的代码,如Directory.GetFiles(mydir).ToList().ForEach(f => File.Copy(f, otherdir + "\\" f);
这不起作用。 –
你不能。 Directory
和DirectoryInfo
均未提供Copy
方法。你需要自己实现这个。
void Copy(string sourceDir, string targetDir)
{
Directory.CreateDirectory(targetDir);
foreach(var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));
foreach(var directory in Directory.GetDirectories(sourceDir))
Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
}
请阅读注意事项以了解此简单方法的一些问题。
MSDN有这个指导 - How to:Copy Directories
执行xcopy source_directory\*.* destination_directory
作为外部命令。当然这只会在Windows机器上运行。
如果不需要,你不应该使用系统调用。在这里,这绝对没有必要。 –
有必要给出这个问题陈述**与循环每个文件**。 – m0skit0
你认为xcopy在做什么?他只是不想循环,因为他认为有一个更简单的方法。没有。一般而言,系统调用既不容易,也不是一个好方法。强烈不鼓励! –
您可以使用VB的FileSystem.CopyDirectory方法来简化任务:
using Microsoft.VisualBasic.FileIO;
foo(){
FileSystem.CopyDirectory(directoryPath, tempPath);
}
我想念VB。人们说你可以在C#中完成所有相同的事情。 –
Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"resources\html")
.ToList()
.ForEach(f => File.Copy(f, folder + "\\" + f.Substring(f.LastIndexOf("\\"))));
using System.IO;
string sourcePath = @"D:\test";
string targetPath = @"D:\test_new";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
//Copy the file from sourcepath and place into mentioned target path,
//Overwrite the file if same file is exist in target path
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}
这个伟大的工程!它将复制子目录,或者您可以将所有子目录中的所有文件转储到一个位置。
/// AUTHOR : Norm Petroff
/// <summary>
/// Takes the files from the PathFrom and copies them to the PathTo.
/// </summary>
/// <param name="pathFrom"></param>
/// <param name="pathTo"></param>
/// <param name="filesOnly">Copies all files from each directory to the "PathTo" and removes directory.</param>
static void CopyFiles(String pathFrom, String pathTo, Boolean filesOnly)
{
foreach(String file in Directory.GetFiles(pathFrom))
{
// Copy the current file to the new path.
File.Copy(file, Path.Combine(pathTo, Path.GetFileName(file)), true);
// Get all the directories in the current path.
foreach (String directory in Directory.GetDirectories(pathFrom))
{
// If files only is true then recursively get all the files. They will be all put in the original "PathTo" location
// without the directories they were in.
if (filesOnly)
{
// Get the files from the current directory in the loop.
CopyFiles(directory, pathTo, filesOnly);
}
else
{
// Create a new path for the current directory in the new location.
var newDirectory = Path.Combine(pathTo, new DirectoryInfo(directory).Name);
// Copy the directory over to the new path location if it does not already exist.
if (!Directory.Exists(newDirectory))
{
Directory.CreateDirectory(newDirectory);
}
// Call this routine again with the new path.
CopyFiles(directory, newDirectory, filesOnly);
}
}
}
}
- 1. 在目录的所有文件夹中复制文件
- 2. 只有在复制完所有目录后才复制文件
- 3. 复制所有文件和目录及子目录SAS
- 4. PHP将目录中的所有文件复制到另一个目录中?
- 5. virtualenv不会复制lib/python目录中的所有.py文件
- 6. 如何将文件复制到tcsh中的所有子目录
- 7. 将目录中的所有文件复制到另一个
- 8. 使用Ansible移动/复制目录中的所有文件
- 9. 将列表中的所有文件复制到唯一目录
- 10. 从所有子目录中复制最新的文件
- 11. 如何使用globstar复制目录中的所有文件?
- 12. 复制所有子目录中具有特定扩展名的所有文件
- 13. 复制目录与所有文件和文件夹
- 14. 如何将目录A中的所有* .bak文件复制到目录B?
- 15. C#,将多个所有子目录中的文件复制到一个目录
- 16. 移动从子目录中的所有文件复制到主目录
- 17. 使用Grunt.js复制目录中的所有文件到另一个复制
- 18. 将文件复制到所有子目录中php
- 19. 将目录中所有文件的第n列复制到一个文件中
- 20. 如何复制文件和所有他的路径的目录
- 21. Bash:将文件的所有参数复制到目录
- 22. 监控触发文件并复制该目录中的所有文件
- 23. 将文件中的所有文件/子目录复制到另一个
- 24. 复制匹配字符串和所有子目录和文件的目录
- 25. Gulp任务复制目录中的所有文件,编译所有SCSS文件,保持其他文件不变?
- 26. Tessdata没有在文件目录复制
- 27. 复制目录覆盖现有文件
- 28. Python 3.4找到所有文件类型并复制到目录
- 29. 从一个FTP目录复制所有文件到另一个
- 30. 递归复制所有文件和目录
看吧:http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results并在那里住了命令“副本\ *。* YOURDIR“ – fritzone