Ready in .NET in a simpler way, which can be done for both extensions is this:
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.xls");
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + files.Length.ToString(), "Alerta");
The ideal would be to specify the two extensions individually but there is nothing ready in .NET. Anything that starts with "xls" and has one more character will be considered, so there may be an undesirable side effect, for example catching the "xlsh" files you eventually have, it's unlikely but it can happen. p>
A better option for .NET 4.5:
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = Directory.EnumerateFiles(path)
.Where(file => file.ToLower().EndsWith("xls") ||
file.ToLower().EndsWith("xlsx"))
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + files.Length.ToString(), "Alerta");
Retired from jgauffin's answer in OS .
If you can not use .NET 4.5 just change the EnumerateFiles
method to GetFiles
, the only disadvantage is performance since the second does not use lazy evaluation .
I still found this option:
Multiple Filters On Directory.GetFiles Method
Simplifying the example and adapting to your case:
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.xls");
files.AddRange(Directory.GetFiles(fbd.SelectedPath, "*.xlsx"));
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + files.Length.ToString(), "Alerta");
Or simplifying and maintaining the generality:
public static string[] GetFiles(string sourceFolder, string filters, System.IO.SearchOption searchOption) {
return filters.Split('|').SelectMany(filter =>
System.IO.Directory.GetFiles(sourceFolder, filter, searchOption)).ToArray();
}
Source: Albert's answer in SO
Although I do not know if this is necessary. See the documentation indicating that "xlsx" is also caught.
When you use the asterisk wildcard character in a searchPattern such as "* .txt", the number of characters in the specified extension affects the search as follows:
-
If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "* .xls" returns both "book.xls" and "book.xlsx".
-
In all other cases, the method returns files that exactly match the specified extension. For example, " .ai" returns "file.ai" but not "file.aif".
When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file? .Txt" returns just the first file, whereas a search pattern of "file .txt "returns both files.
As there is this behavior I do not know if any solution is ideal other than to do the crazy thing to create a own system of capture of the files and a filter in with the own criteria. Hardly worth the effort. So I think the second solution using LINQ turns out to be the best.
I placed it on GitHub for future reference.