C# Get All Files from a Folder:
This post shows how to get list of file names from a directory (including subdirectories). You can filter the list by specific extension.
To get file names from the specified directory, use static method Directory.GetFiles. Lets have these files and subfolders in "d:\YourDir" folder:
Get files from directory
Method Directory.GetFiles returns string array with files names (full paths).
C# Code
Get files from directory (with specified extension)
You can specify search pattern. You can use wildcard specifiers in the search pattern, e.g. „*.png“ to select files with the extension or „a*“ to select files beginning with letter, a“.
C# Code
Get files from directory (including all subdirectories)
If you want to search also in subfolders use parameter Search Option. All Directories.
C# Code
This post shows how to get list of file names from a directory (including subdirectories). You can filter the list by specific extension.
To get file names from the specified directory, use static method Directory.GetFiles. Lets have these files and subfolders in "d:\YourDir" folder:
Get files from directory
Method Directory.GetFiles returns string array with files names (full paths).
C# Code
using System.IO;
string[] filePaths = Directory.GetFiles(@"d:\Images\"); // returns: // "d:\Images\dotnetcluster.png" // "d:\Images\home.jpg"
Get files from directory (with specified extension)
You can specify search pattern. You can use wildcard specifiers in the search pattern, e.g. „*.png“ to select files with the extension or „a*“ to select files beginning with letter, a“.
C# Code
string[] filePaths = Directory.GetFiles(@"d:\Images\", "*.png");
// returns:
// "c:\Images\dotnetcluster.png"
Get files from directory (including all subdirectories)
If you want to search also in subfolders use parameter Search Option. All Directories.
C# Code
string[] filePaths = Directory.GetFiles(@"d:\Images\", "*.png",
Share this post if it is useful to you. Thanks!.SearchOption.AllDirectories); // returns: // "d:\Images\dotnetcluster.png" // "d:\Images\Posts\header.png"
0 comments
Post a Comment