的Visual C#:移动多个文件具有相同的扩展到另一个目录 [英] Visual C#: Move multiple files with the same extensions into another directory

查看:332
本文介绍了的Visual C#:移动多个文件具有相同的扩展到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家伙。我有一个问题,我解决不了:
我有2个文件夹我的FolderBrowserDialog选择和源目录中的文件吨我需要移动到目标目录中。但是,我只有与特定扩展名状.txt或任何其他分机我可以从文本框获取移动文件。
所以,我该怎么办呢?

guys. I've got a problem I can't solve: I have a 2 folders I choose with folderBrowserDialog and tons of files in source directory I need to move to the target directory. But, I have only to move files with a specific extension like .txt or any other extension I can get from textbox. So how can I do it?

推荐答案

使用所有指定的扩展名的文件的 Directory.GetFiles()然后通过列表中的每个文件迭代,将它们移动到目标目录

First get all the files with specified extension using Directory.GetFiles() and then iterate through each files in the list and move them to target directory.

//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)));
}

这篇关于的Visual C#:移动多个文件具有相同的扩展到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆