How to use Filter in FolderBrowserDialog

4

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.

    
asked by anonymous 15.12.2014 / 16:52

2 answers

3

Maybe your problem is not in the directory selection, but in the files. I already answered something like this in another answer . You should use Directory.GetFiles() with filter options:

public List<string> listaArquivos(string dir)
{
    List<string> lstDirs = Directory.GetDirectories(dir).ToList();
    List<string> lstFiles = Directory.GetFiles(dir, "*.frm", SearchOption.AllDirectories).ToList();
    List<string> lstFilesAux = new List<string>();
    foreach(string ldir in lstDirs)
        lstFilesAux = listaArquivos(ldir);

    lstFiles.AddRange(lstFilesAux);
    return lstFiles;
}

I placed GitHub for future reference .

I still have questions if the rest of the code is doing what you want. But without further information I may be wrong. Maybe what you want to do is something simpler:

private void btnDiretorio_Click(object sender, EventArgs e) {
    this.grvShowFile.Rows.Clear();
    folderBrowserDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory;
    folderBrowserDialog.SelectedPath = openFileDialog.InitialDirectory;
    openFileDialog.Filter = "Form (*.frm)|*.frm"; //Filtro de arquivos a serem selecionados
    DialogResult result = folderBrowserDialog.ShowDialog();
    if (result == DialogResult.OK) {
        List<string> selectedPath = Directory.GetFiles(folderBrowserDialog.SelectedPath,
                                         "*.frm", SearchOption.AllDirectories).ToList();
        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() )
    }
}

I placed GitHub for future reference .

    
15.12.2014 / 17:16
2

Well, since my example worked too, I think it's a bit better than using recursion.

public List<string> listaArquivos(string dir)
    {
        List<string> lstFiles = Directory.GetFiles(dir, "*.frm", SearchOption.AllDirectories).ToList();         
        return lstFiles;
    }
    
15.12.2014 / 17:19