Getting all file names from a folder using C#

user2061405 picture user2061405 · Feb 14, 2013 · Viewed 501.2k times · Source

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?

Answer

Gopesh Sharma picture Gopesh Sharma · Feb 14, 2013
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...