使用 C# 从文件夹中获取所有文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14877237/
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-10 13:18:44  来源:igfitidea点击:

Getting all file names from a folder using C#

c#listtext-filesdirectory

提问by user2061405

I wanted to know if it is possible to get all the names of text files in a certain folder.

我想知道是否可以获取某个文件夹中文本文件的所有名称。

For example, I have a folder with the name Maps, and I would like to get the names of all the text files in that folder and add it to a list of strings.

例如,我有一个名为 Maps 的文件夹,我想获取该文件夹中所有文本文件的名称并将其添加到字符串列表中。

Is it possible, and if so, how I can achieve this?

是否有可能,如果有,我该如何实现?

回答by rerun

Does exactly what you want.

正是你想要的。

System.IO.Directory.GetFiles

System.IO.Directory.GetFiles

回答by Avitus

using System.IO; //add this namespace also 


string[] filePaths = Directory.GetFiles(@"c:\Maps\", "*.txt",
                                         SearchOption.TopDirectoryOnly);

回答by RainbowFish

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

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

The System.IO namespace has loads of methods to help you with file operations. The

System.IO 命名空间有很多方法可以帮助您进行文件操作。这

Directory.GetFiles() 

method returns an array of strings which represent the files in the target directory.

方法返回一个字符串数组,表示目标目录中的文件。

回答by Sl1ver

I would recommend you google 'Read objects in folder'. You might need to create a reader and a list and let the reader read all the object names in the folder and add them to the list in n loops.

我建议你谷歌“读取文件夹中的对象”。您可能需要创建一个阅读器和一个列表,让阅读器读取文件夹中的所有对象名称,并在 n 次循环中将它们添加到列表中。

回答by James Culshaw

Take a look at Directory.GetFiles Method (String, String)(MSDN).

看看Directory.GetFiles 方法(字符串,字符串)(MSDN)。

This method returns all the files as an array of filenames.

此方法将所有文件作为文件名数组返回。

回答by Gopesh Sharma

DirectoryInfo d = new DirectoryInfo(@"D:\Test");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files
string str = "";
foreach(FileInfo file in Files )
{
  str = str + ", " + file.Name;
}

Hope this will help...

希望这会有所帮助...

回答by Gawie Greef

It depends on what you want to do.

这取决于你想做什么。

ref: http://www.csharp-examples.net/get-files-from-directory/

参考:http: //www.csharp-examples.net/get-files-from-directory/

This will bring back ALL the files in the specified directory

这将带回指定目录中的所有文件

string[] fileArray = Directory.GetFiles(@"c:\Dir\");

This will bring back ALL the files in the specified directory with a certain extension

这将带回指定目录中具有特定扩展名的所有文件

string[] fileArray = Directory.GetFiles(@"c:\Dir\", "*.jpg");

This will bring back ALL the files in the specified directory AS WELL AS all subdirectories with a certain extension

这将带回指定目录中的所有文件以及具有特定扩展名的所有子目录

string[] fileArray = Directory.GetFiles(@"c:\Dir\", "*.jpg", SearchOption.AllDirectories);

Hope this helps

希望这可以帮助