How to filter files when listing directories in C#
I am trying to filter outout of a method, so only files with a "FF-" prefix will be returned.
My code is as follows:
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Windows\system32\tasks");
FileInfo[] Files = dinfo.GetFiles("*.*");
foreach (FileInfo file in 开发者_高级运维Files)
{
listBox1.Items.Add(file.Name);
}
Like this?
FileInfo[] Files = dinfo.GetFiles("FF-*.*");
Directory.GetFiles Method
Quote:
* - Zero or more characters.
? - Exactly zero or one character.
For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".
You can apply search pattern "FF-*"
-or "FF-*.txt"
for .txt
files only-, however if want to get the files paths only then using Directory.GetFiles
is better choice
string[] files = Directory.GetFiles(@"C:\Windows\system32\tasks", "FF-*.*");
foreach (string filePath in Files)
{
listBox1.Items.Add(file.Name);
}
have you tried this?
FileInfo[] Files = dinfo.GetFiles("FF-*.*");
精彩评论