Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How can I get the list of files in a directory using C?

Call opendir() function to open all file in present directory. Initialize dr pointer as dr = opendir("."). If(dr) while ((en = readdir(dr)) != NULL) print all the file name using en->d_name.

How do I get a list of files in a directory and subfolders?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.


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

Hope this will help...


using System.IO; //add this namespace also 

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

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


Does exactly what you want.

System.IO.Directory.GetFiles


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

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