301

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?

0

7 Answers 7

473
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;
}
4
  • 61
    Using System.IO;
    – Jeff
    Sep 11, 2014 at 13:20
  • 13
    FileInfo has a lot of overhead, it would be much better to use Path.GetFileName(filePath) Dec 4, 2014 at 22:59
  • 1
    How does DirectoryInfo and FileInfo compare to the Directory.GetFiles approach? Dec 6, 2018 at 22:17
  • 1
    @Jeff Please be under some answers, where it's not obvious which namespaces are used :D Thx.
    – arsdever
    Mar 26, 2021 at 12:01
211
using System.IO; //add this namespace also 

string[] filePaths = Directory.GetFiles(@"c:\Maps\", "*.txt",
                                         SearchOption.TopDirectoryOnly);
2
  • 3
    How does Directory.GetFiles compare to the DirectoryInfo and FileInfo approach? Dec 6, 2018 at 22:16
  • 8
    @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.
    – Pona
    Dec 16, 2018 at 19:29
89

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

18

Does exactly what you want.

System.IO.Directory.GetFiles

2
  • 1
    IO is capitalized. Dec 7, 2018 at 4:08
  • 1
    Around here we discourage simply linking to the manual. Next time you should also paste in the relevant information with your answer here.
    – Alpants
    Nov 6, 2019 at 15:27
9

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

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

8

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.

-10

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.

1
  • 12
    Much more efficient to use the inbuilt GetFiles method on the Directory class. Feb 14, 2013 at 14:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.