4
I would like to know how do I put a filter on certain files using c#.
Good is more or less like this, I am making an application in c# that should open some types of files ( in the case .frm ), in the "manual" part which would be when the user selects 1 or more files to be loaded and passed in Datagridview he is using the filter without major problems .
openFileDialog.Filter = "Form (*.frm)|*.frm|" + "All files (*.*)|*.*"; //Filtro de arquivos a serem selecionados
Note: I am using openFileDialog to make manual file selection.
In the second part ( that is the selection of a folder or a subfolder ) is the one that I have problems with when using this filter. I had already asked another question here that was
"How to pass filenames to a Datagridview using Folderbrowserdialog"
Now my other question is: How do I use the file selection filter in Folderbrowserdialog. Using Openfiledialog he already gave you this option to use the filter - as it is seen up there in my example - but in Folderbrowserdialog he does not give you this option to put a filter. I had already tried to make the filter in the same way as my example but it didn’t work. Follow the code part where I’m using Folderbrowserdialog.
private void btnDiretorio_Click(object sender, EventArgs e)
{
this.grvShowFile.Rows.Clear();
folderBrowserDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory;
folderBrowserDialog.SelectedPath = openFileDialog.InitialDirectory;
folderBrowserDialog.ShowNewFolderButton = true;
openFileDialog.Filter = "Form (*.frm)|*.frm"; //Filtro de arquivos a serem selecionados
DialogResult result = folderBrowserDialog.ShowDialog();
if (result == DialogResult.OK)
{
List<string> selectedPath = listaArquivos(folderBrowserDialog.SelectedPath);
foreach (string s in selectedPath)
{
grvShowFile.Rows.Add(Path.GetFileName(s), s); //Adiciona o nome e o caminho dos arquivos nas respectivas ordens ( Bendito seja o Path.GetFileName() )
}
}
}
public List<string> listaArquivos(string dir)
{
List<string> lstDirs = Directory.GetDirectories(dir).ToList();
List<string> lstFiles = Directory.GetFiles(dir).ToList();
List<string> lstFilesAux = new List<string>();
foreach(string ldir in lstDirs)
lstFilesAux = listaArquivos(ldir);
lstFiles.AddRange(lstFilesAux);
return lstFiles;
}
Note: I used a List<>
to check the directory and check the subdirectories if you have any.
Well I hope you explained my doubt well, every explanation is super welcome here.
List<string> lstDirs = Directory.GetDirectories(dir, "*.frm", SearchOption.AllDirectories).ToList();
, see if it helps you– Pablo Tondolo de Vargas
No friend, he’s still picking up all the files from inside the directory.
– Rodolfo Olivieri
It was bad, I missed the method I had to change try the following
public List<string> listaArquivos(string dir)
 {
 List<string> lstFiles = Directory.GetFiles(dir, "*.frm", SearchOption.AllDirectories).ToList(); 
 return lstFiles;
 }
– Pablo Tondolo de Vargas
Thanks now it worked out !!
– Rodolfo Olivieri
@Rodolfoolivieri 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? Need something to be improved?
– Maniero