ChatGPT解决这个技术问题 Extra ChatGPT

Getting all file names from a folder using C# [duplicate]

This question already has answers here: How to recursively list all the files in a directory in C#? (23 answers) Closed 9 years ago.

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.

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


A
Anye
using System.IO;

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

Using System.IO;
FileInfo has a lot of overhead, it would be much better to use Path.GetFileName(filePath)
How does DirectoryInfo and FileInfo compare to the Directory.GetFiles approach?
@Jeff Please be under some answers, where it's not obvious which namespaces are used :D Thx.
H
Hammad Sajid
using System.IO; //add this namespace also 

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

How does Directory.GetFiles compare to the DirectoryInfo and FileInfo approach?
@AaronFranke Directory.GetFiles will give you an array of fullpaths of the files contained in the Directory, whereas the DirectoryInfo approach will give you an array of FileInfo, which contains more info about each file, such as filename, extension, size, modified time, etc.
G
Gawie Greef

It depends on what you want to do.

ref: 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


r
rerun

Does exactly what you want.

System.IO.Directory.GetFiles


IO is capitalized.
Around here we discourage simply linking to the manual. Next time you should also paste in the relevant information with your answer here.
P
Peter Mortensen

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

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


R
RainbowFish

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

Directory.GetFiles() 

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


P
Peter Mortensen

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.


Much more efficient to use the inbuilt GetFiles method on the Directory class.