
C# directory getfiles get all and multiple specified extensions, with search pattern
Directoryinfo can get all folders or files; directoryinfo GetDirectories() can get all folders in the specified directory, and directoryinfo GetFiles() can get all files in the specified directory.
Sometimes writing code to get several specified types of files, for example, to get all video files in a specified directory, video files have many formats, such as rmvb, rm, avi, wav, mkv, mp4, etc., but directoryinfo GetFiles() can only be passed one format parameter at a time, so you have to get them separately. Before introducing the got method, first look at some basic usages of directoryinfo GetFiles().
1. C# directory getfiles get all extensions
If you want to get all the files in the specified directory, you can use the following code:
/// <summary>
/// Get all extensions
/// </summary>
/// <param name="filePath">Source path</param>
private void GetFiles(string filePath)
{
DirectoryInfo di = new DirectoryInfo(filePath);
FileInfo[] afi = di.GetFiles("*.*");//*.* can not
}
Call: GetFiles(@"F:\fi\test");
2. C# directory getfiles get a file with a specified extension
If you want to get all the .dat files in the specified directory, the same method is used, except that *.* is changed to *.dat. The code is as follows:
/// <summary>
/// Get a file with a specified extension
/// </summary>
/// <param name="filePath">Source path</param>
private void GetFiles(string filePath)
{
DirectoryInfo di = new DirectoryInfo(filePath);
FileInfo[] afi = di.GetFiles("*.dat");
}
Call: GetFiles(@"F:\fi\test");
3. C# getfiles multiple extensions
Since getfiles() can only get one type of file at a time, you can only get all the files in the specified directory first, recycle to determine whether each file is the file to be got; if you get all the video files in the specified directory, the implementation code is as follows:
/// <summary>
/// C# getfiles multiple extensions
/// </summary>
/// <param name="filePath">Source path</param>
private void GetVideoFiles(string filePath)
{
DirectoryInfo di = new DirectoryInfo(filePath);
FileInfo[] afi = di.GetFiles("*.*");
string fileName;
IList<string> list = new List<string>();
for (int i = 0; i < afi.Length; i++)
{
fileName = afi[i].Name.ToLower();
if (fileName.EndsWith(".rmvb") || fileName.EndsWith(".rm") || fileName.EndsWith(".avi") || fileName.EndsWith(".mp4"))
{
list.Add(fileName);
}
}
}
If different types of files require different operations, change the parallel conditions to if(){} else if(){} or switch statement.
Call: GetVideoFiles(@"F:\fi\movies");
4. C# getfiles search pattern
The getfiles() can specify the search pattern to fuzzyly find the specified file, and there are *, ?, etc. used in regular expressions to represent any character symbols in the search conditions. The implementation code is as follows:
/// <summary>
/// Get multiple specified files in the specified directory in the specified search pattern
/// </summary>
/// <param name="filePath"> Source path </param>
/// <param name="searchPattern"> Search pattern </param>
/// <param name="searchOption"> Search option </param>
/// <returns> Got files</returns>
private ArrayList GetFiles(string filePath, string searchPattern, SearchOption searchOption)
{
if (string.IsNullOrEmpty(searchPattern))
return null;
string[] arrExt = searchPattern.Split('|');
ArrayList arrLstFiles = new ArrayList();
for (int i = 0; i < arrExt.Length; i++)
{
arrLstFiles.AddRange(Directory.GetFiles(filePath, arrExt[i], searchOption));
}
return arrLstFiles;
}
Call(if you want to get all Word and Excel documents and jpg pictures in a folder):
ArrayList al = GetFiles(@"F:\fi\doc", "*.*x|*.jpg", SearchOption.TopDirectoryOnly);
for (int i = 0; i < al.Count; i++)
Response.Write(al[i] + "<br />");
-
Related Reading
- C# Read and write to text file Newline, with one lin
- Bubble sort algorithm example in C# and C (Ascending
- C# if else vs switch, which efficiency is high(multi
- C# label control, with winforms label transparent ba
- C# Richtextbox change font color, add and foreach li
- C# sort files or folders by name, date and creation
- Three methods of Hashtable sort by key or value in C
- C# Windows Forms foreach controls, with Textbox and
- C# sorted dictionary by value, key or length of the
- C# Winforms panel border, with color, style, thickne
- C# Winforms listview show table(adaptive column widt
- Selection sort algorithm C# and C (Ascending and Des