[英]How do i copy files from one directory to another directory?
string[] s = Directory.GetFiles(t, "*.txt",SearchOption.AllDirectories);
for (int i = 0; i < s.Length; i++)
{
File.Copy(s[i],
}
File.Copy will copy the files to another file name. File.Copy会将文件复制到另一个文件名。 I want to keep the same files names just copy them from one directory to another directory. 我想保持相同的文件名,只需将它们从一个目录复制到另一个目录即可。
用这个:
File.Copy(s[i], "c:\\anotherFolder\\" + Path.GetFileName(s[i]));
You can do it nicely like this: 您可以像这样很好地做到:
Directory.GetFiles("c:\\temp", "*.txt", SearchOption.AllDirectories) // get the files
.Select(c => new FileInfo(c)) // project each filename into a fileinfo
.ToList() // convert to list
.ForEach(c => c.CopyTo("d:\\temp\\" + c.Name)); // foreach fileinfo, copy to the desired path + the actual file name
You could view this post, which should help; 您可以查看这篇文章,这应该有所帮助;
Code snippet: 程式码片段:
var sourceDir = @"c:\sourcedir";
var destDir = @"c:\targetdir";
var pattern = "*.txt";
foreach (var file in new DirectoryInfo(sourceDir).GetFiles(pattern))
{
file.CopyTo(Path.Combine(destDir, file.Name));
}
Hope this helps? 希望这可以帮助?
You are doing it the right way.. 您做对了。
See example from microsoft 请参阅Microsoft的示例
http://msdn.microsoft.com/en-us/library/bb762914.aspx http://msdn.microsoft.com/en-us/library/bb762914.aspx
another solution might be to call the xcopy command ... 另一个解决方案可能是调用xcopy命令...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.