移动所有文件,子文件夹到另一个文件夹使用C# [英] Move all files in subfolders to another folder using c#

查看:543
本文介绍了移动所有文件,子文件夹到另一个文件夹使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的源路径是 C:\Music\ 中,我有数以百计的所谓专辑​​-1文件夹,专辑等2

My source path is C:\Music\ in which I have hundreds of folders called Album-1, Album-2 etc.

我想要做的就是创建一个在我的源路径名为合并文件夹中。结果
,然后我想我的专辑里面的所有文件移动到文件夹合并,让我得到一个文件夹中的所有音乐文件。

What I want to do is create a folder called Consolidated in my source path.
And then I want to move all the files inside my albums to the folder Consolidated, so that I get all the music files in one folder.

我怎样才能做到这一点?

How can I do this ?

推荐答案

试试这样

String directoryName = "C:\\Consolidated";
DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
if (dirInfo.Exists == false)
    Directory.CreateDirectory(directoryName);

List<String> MyMusicFiles = Directory
                   .GetFiles("C:\\Music", "*.*", SearchOption.AllDirectories).ToList();

foreach (string file in MyMusicFiles)
{
    FileInfo mFile = new FileInfo(file);
    // to remove name collusion
    if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false) 
         mFile.MoveTo(dirInfo + "\\" + mFile.Name);
}



这将得到所有的文件在C:\Music文件夹中(包括子文件夹的文件),并将其移动到目标文件夹。在 SearchOption.AllDirectories 将递归搜索所有子文件夹。

It will get all the files in the "C:\Music" folder (including files in the subfolder) and move them to the destination folder. The SearchOption.AllDirectories will recursively search all the subfolders.

这篇关于移动所有文件,子文件夹到另一个文件夹使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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