Ready on . NET more simply, 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 the .NET. The way it is anything that starts with "xls" and has any more character will be considered, so there may be an unwanted side effect, for example grab the files "xlsh" that eventually you have, is unlikely but can happen.
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");
Taken from jgauffin response in the OS.
If you cannot use . NET 4.5 simply change the method EnumerateFiles()
for GetFiles()
, the only downside is the 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 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 response in the OS
Although I don’t know if it’s necessary to do this. See the documentation indicating that the "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 except to do the madness of creating a system own capture of the files and a filter na with own criteria. Hardly worth the effort. So I think the second solution using LINQ turns out to be the best.
I put in the Github for future reference.
Congsegui partially ! In the array I put the following command. string[] files = System.IO.Directory.Getfiles(fbd.Selectedpath, "*.xlsx"); It is checking only xlsx files, I need it to check xls as well.
– Leandro Díaz
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero