c# - 视觉 C# : Move multiple files with the same extensions into another directory

标签 c# file move

伙计们。我有一个我无法解决的问题: 我有 2 个文件夹,我选择了 folderBrowserDialog 和源目录中的大量文件,我需要 move 到目标目录。但是,我只需要 move 具有特定扩展名的文件,例如 .txt 或我可以从文本框获得的任何其他扩展名。 那我该怎么做呢?

最佳答案

首先使用 Directory.GetFiles() 获取所有具有指定扩展名的文件然后遍历列表中的每个文件并将它们 move 到目标目录。

//Assume user types .txt into textbox
string fileExtension = "*" + textbox1.Text;

string[] txtFiles = Directory.GetFiles("Source Path", fileExtension);

foreach (var item in txtFiles)
{
   File.Move(item, Path.Combine("Destination Directory", Path.GetFileName(item)));
}

关于c# - 视觉 C# : Move multiple files with the same extensions into another directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424844/

相关文章:

java - 无法在文件浏览器中查看所有文件

c++ - 编译器什么时候不能使用 RVO 或 NRVO?

powershell - 将 31 天之前的文件 move 到另一个驱动器

c# - 反射用列表c#修剪列表中的所有字符串

c# - 在 .Net 应用程序中包含单元测试时的设计注意事项?

c# - 要发布哪些文件? ASP .Net 核心 2.0

c - 如何在C中读取和覆盖文本文件?

Django 提供生成的 excel 文件

Jquery move 表行但不 move 表头

c# - 进行 SSL 连接时需要主机名验证