Getting all file names from a folder using C #

I wanted to know if it is possible to get all the text file names in a specific folder.

For example, I have a folder called Maps, and I would like to get the names of all the text files in this folder and add them to the list of lines.

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

+125
list c # folder text-files folders
Feb 14 '13 at 14:39
source share
7 answers
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 helps ...

+235
Feb 14 '13 at 14:43
source share
 using System.IO; //add this namespace also 



 string[] filePaths = Directory.GetFiles(@"c:\Maps\", "*.txt", SearchOption.TopDirectoryOnly); 
+89
Feb 14 '13 at 14:41
source share

It depends on what you want to do.

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

This will return ALL files in the specified directory

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

This will return ALL files in the specified directory with a specific extension

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

This will return ALL files in the specified AS WELL AS directory in all subdirectories with a specific extension

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

Hope this helps

+53
Feb 14 '13 at 15:32
source share

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

This method returns all files as an array of file names.

+7
Feb 14 '13 at 14:42
source share

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

The System.IO namespace has many methods to help you work with files.

 Directory.GetFiles() 

The method returns an array of strings that represent the files in the destination directory.

+6
Feb 14 '13 at 14:41
source share

Does exactly what you want.

System.IO.Directory.GetFiles

+3
Feb 14 '13 at 14:41
source share

I would recommend you google "Read objects in a folder". You may need to create a reader and a list, and let the reader read all the names of the objects in the folder and add them to the list in n loops.

-5
Feb 14 '13 at 14:42
source share



All Articles