I would like to know how do I put a filter on certain files using C #.
Well, it's more or less like this, I'm doing an application in C # that should open some types of files (in the case .frm), in the "manual" part that would be when the user selects 1 or more files to load and passed in the DataGridView it is using the filter without major problems.
openFileDialog.Filter = "Form (*.frm)|*.frm|" + "All files (*.*)|*.*"; //Filtro de arquivos a serem selecionados
Note: I'm using openFileDialog to manually select the files.
In the second part (which is the selection of a folder or subfolder) is the one that I'm having problems using this filter. I had already asked another question here that was
" How to pass names of a file to a DataGridView using FolderBrowserDialog "
Now my other question is: How do I use the file selection filter in FolderBrowserDialog. Using OpenFileDialog it already gave you this option to use the filter - as seen above in my example - but already in FolderBrowserDialog it does not give you that option to put a filter. I had already tried to make the filter the same way as my example above but it did not work. Here's the part of the code I'm using in 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've used List<>
to do directory checking and subdirectories checking if any.
Well I hope you have explained my question well, any explanation is welcome here.