简体   繁体   English

如何将文件从一个目录复制到另一目录?

[英]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; 您可以查看这篇文章,这应该有所帮助;

Or this MSDN link: 此MSDN链接:

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.

相关问题 如何使用c#窗口形式的progressBar将多个文件从一个目录复制到另一个目录 - how to copy multiple files from one directory to another with progressBar in c# window form 将文件从列表框复制到另一个目录 - Copy files from listbox to another directory 如何使用AWS api将一个目录的内容复制到另一个目录? - How can I copy contents of one directory to another with the AWS api? 将文件从一个目录复制到另一个目录 - Copy file from one directory to another 如何使用 FileDialog 创建目录,然后自动将文件复制到新目录? - How do I use FileDialog to create a directory, then automatically copy files to new directory? 如何在 Unity Hololens 中将文件从一个目录移动到另一个目录 - How to move files from one directory to another in Unity Hololens 如何将所有* .bak文件从目录A复制到目录B? - how to copy all *.bak files from Directory A to Directory B? 如何将checkedListBox中列出的目录复制到另一个目录? - How to copy directory listed in checkedListBox to another directory? 将Blob从一个目录复制到同一容器Azure存储中的另一个目录 - Copy blob from one directory into another in same container Azure Storage 将exe从一个项目复制到另一个项目的调试输出目录 - Copy exe from one project to another's debug output directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM