C# 查找文件夹中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11861151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:32:26  来源:igfitidea点击:

Find all files in a folder

c#filedirectory

提问by Oliver K

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.

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

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

任何想法是实现这一目标的最佳方法是什么?谢谢。

I have tried this:

我试过这个:

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.

未能成功找到所有文件。

采纳答案by dtsg

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

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

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

回答by Aghilas Yakoub

You can try with Directory.GetFiles and fix your pattern

您可以尝试使用 Directory.GetFiles 并修复您的模式

 string[] files = Directory.GetFiles(@"c:\", "*.txt");

 foreach (string file in files)
 {
    File.Copy(file, "....");
 }

 Or Move

 foreach (string file in files)
 {
    File.Move(file, "....");
 }     

http://msdn.microsoft.com/en-us/library/wz42302f

http://msdn.microsoft.com/en-us/library/wz42302f

回答by Gerald Versluis

First off; best practice would be to get the users Desktop folder with

首先; 最佳做法是获取用户桌面文件夹

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

Then you can find all the files with something like

然后你可以找到所有类似的文件

string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);

Note that with the above line you will find all files with a .txt extension in the Desktop folder of the logged in user AND all subfolders.

请注意,使用上面的行,您将在登录用户的桌面文件夹和所有子文件夹中找到所有扩展名为 .txt 的文件。

Then you could copy or move the files by enumerating the above collection like

然后,您可以通过枚举上述集合来复制或移动文件,例如

// For copying...
foreach (string s in files)
{
   File.Copy(s, "C:\newFolder\newFilename.txt");
}

// ... Or for moving
foreach (string s in files)
{
   File.Move(s, "C:\newFolder\newFilename.txt");
}

Please note that you will have to include the filename in your Copy()(or Move()) operation. So you would have to find a way to determine the filename of at least the extension you are dealing with and not name all the files the same like what would happen in the above example.

请注意,您必须在Copy()(或Move())操作中包含文件名。因此,您必须找到一种方法来确定至少您正在处理的扩展名的文件名,而不是像上面的示例中那样为所有文件命名。

With that in mind you could also check out the DirectoryInfoand FileInfoclasses. These work in similair ways, but you can get information about your path-/filenames, extensions, etc. more easily

考虑到这一点,您还可以查看DirectoryInfoFileInfo类。这些以类似的方式工作,但您可以更轻松地获取有关路径/文件名、扩展名等的信息

Check out these for more info:

查看这些以获取更多信息:

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

http://msdn.microsoft.com/en-us/library/ms143316.aspx

http://msdn.microsoft.com/en-us/library/ms143316.aspx

http://msdn.microsoft.com/en-us/library/system.io.file.aspx

http://msdn.microsoft.com/en-us/library/system.io.file.aspx