Get Files from Directory
![File Structure](57f135a6-229f-4c27-a2f7-1b49c614874e_files/get-files-from-directory-file-structure[1].png)
Get Files from Directory [C#]
This example shows how to get list of file names from a directory (includingsubdirectories).
You can filter the list by specific extension.
To get file names from the specified directory, use static method
Lets have these files andsubfolders in „c:\MyDir“ folder:
Get files from directory
Method Directory.GetFiles returns string array with files names (fullpaths).
[C#] using System.IO;
string[] filelist = Directory.GetFiles(@"D:\迅雷下载");
foreach (var f in filelist)
richTextBox1.AppendText(f + "\r\n"); ![](57f135a6-229f-4c27-a2f7-1b49c614874e_files/0fb6a50d-0093-416a-be04-ffbdfc36cb22.png)
Get files from directory (with specified extension)
You can specify search pattern. You can use wildcard specifiers in the searchpattern,
e.g. „*.bmp“ to select files with the extension or „a*“ toselect files beginning with letter „a“.
[C#] string[] filelist = Directory.GetFiles(@"D:\迅雷下载\","*.zip");
foreach (var f in filelist)
richTextBox1.AppendText(f + "\r\n"); 获得文件夹下的指定文件列表(只找一层)
![](http://images0.cnblogs.com/blog/300447/201406/221151380679314.png)
Get files from directory (including all subdirectories)
If you want to search also in subfolders use parameterSearchOption.AllDirectories.
[C#] string[] filelist = Directory.GetFiles(@"D:\迅雷下载\", "*.*",SearchOption.AllDirectories);
foreach (var f in filelist)
richTextBox1.AppendText(f + "\r\n");![](57f135a6-229f-4c27-a2f7-1b49c614874e_files/de996cc3-bc20-4263-a5dc-18676690b887.png)