How to use Filter in Folderbrowserdialog

Asked

Viewed 808 times

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

  • No friend, he’s still picking up all the files from inside the directory.

  • It was bad, I missed the method I had to change try the following public List<string> listaArquivos(string dir)&#xA; {&#xA; List<string> lstFiles = Directory.GetFiles(dir, "*.frm", SearchOption.AllDirectories).ToList(); &#xA; return lstFiles;&#xA; }

  • Thanks now it worked out !!

  • @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?

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 must use the 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 put in the Github for future reference.

I still have doubts if the rest of the code is doing what you want. But without more information I could 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 put in the Github for future reference.

  • Thanks for the help, buddy, I tested it here and it worked yours and Pablo’s up there !!

  • What’s the point of recursion? Getfiles won’t search the subdirectories?

  • And what I’ve been asking him since his previous question.

  • At first they had asked me to test recursively and I haven’t changed it yet. I’m just doing tests.

  • The method that uses recursion is better. If there are directories with permissions denied to the current user, the function Directory.GetFiles will make an exception. The only way to treat these exceptions punctually is by navigating to each directory manually, and treating the exceptions related to access permission. If the option that already returns everything is used, if there is a single directory with denied permission in the directory tree, the exception will be thrown, and nothing will be returned.

  • To check what I said earlier, just a simple test: Directory.GetFiles("C:\\", "*.txt", SearchOption.AllDirectories); Man, this is gonna suck for sure!!!

  • The exception to be addressed is the System.UnauthorizedAccessException, when calling Directory.GetFiles, thus allowing you to dodge directories without access allowed.

  • @Miguelangelo I agree. What is difficult is people wanting to do all necessary treatment since almost always she will not have this problem. I would do something more robust but people just want to see it working under ideal conditions.

Show 3 more comments

2

Well, since that example also worked for me, I think it’s a little better than using recursion.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.