查找文件夹中的所有文件 [英] Find all files in a folder

查看:25
本文介绍了查找文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个程序,该程序可以在我的桌面上查找某种类型的所有文件并将它们放入特定文件夹中,例如,我会将所有带有 .txt 的文件放入 Text 文件夹中.

I am looking to create a program that finds all files of a certain type on my desktop and places them into specific folders, for example, I would have all files with .txt into the Text folder.

任何想法最好的方法是什么?谢谢.

Any ideas what the best way would be to accomplish this? Thanks.

我已经试过了:

string startPath = @"%userprofile%/Desktop";
string[] oDirectories = Directory.GetDirectories(startPath, "");
Console.WriteLine(oDirectories.Length.ToString());

foreach (string oCurrent in oDirectories)
    Console.WriteLine(oCurrent);

Console.ReadLine();

未能成功找到所有文件.

It was not successful in finding all of the files.

推荐答案

很多这些答案实际上不起作用,我自己尝试过.试一试:

A lot of these answers won't actually work, having tried them myself. Give this a go:

string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DirectoryInfo d = new DirectoryInfo(filepath);

foreach (var file in d.GetFiles("*.txt"))
{
      Directory.Move(file.FullName, filepath + "\TextFiles\" + file.Name);
}

它会将桌面上的所有 .txt 文件移动到文件夹 TextFiles.

It will move all .txt files on the desktop to the folder TextFiles.

这篇关于查找文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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