How to pass filenames to a Datagridview using Folderbrowserdialog?

Asked

Viewed 593 times

3

How do I pass the name of the files that were found inside the selected folder?

So far I’ve only managed to get him to pick up the file’s path and move on to the DataGridView and now I want to make him pass the name too.

Here’s the code part for the 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|" + "All files (*.*)|*.*";
        DialogResult result = folderBrowserDialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            List<string> selectedPath = listaArquivos(folderBrowserDialog.SelectedPath);
            foreach (string s in selectedPath)
            {
                grvShowFile.Rows.Add(s);
            }
        }
    }

    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;
    }

And if you could also tell me how I make it so that he only retrieves the files that are in the filter I would appreciate it.

  • I don’t understand your problem. The path includes the name of the file. Do you want to get only the name of the file that is within the path? Nor do I understand what is wrong with the second part of the question.

  • Yes, what I want is to take the name of the file, for example: Logs.txt In this code what it is picking is only the path for example "C: Logs.txt", understand? in my Datagridview has these 2 fields one for the file name and the other for the file path and at the moment it is just picking up the file path.

  • Is there anything you need to improve on the answers you received in your questions? You didn’t vote for them or accept any. See [tour] If you think the main objective of this question has been achieved you can accept the answer. You can also vote for everything on the site you find useful, not just things related to your posts.

2 answers

1

To get only the file name contained in a path is used the method Path.GetFileName().

He’ll probably do something like this:

List<string> selectedPath = listaArquivos(folderBrowserDialog.SelectedPath);
foreach (string s in selectedPath) {
    grvShowFile.Rows.Add(Path.GetFileName(s));
}

I put in the Github for future reference.

  • Thanks I now it’s taking both the name and the path :) and the file restriction part? How can I do this?

  • I don’t understand what you want to do with it, I think you’re already doing it but you seem to want something else. Try to explain better what is going wrong and what is missing to do.

  • At the moment he is picking up all the files inside the folder. The function is just accessing the directory and subdirectories and taking everything that’s in there. What I want from my application is: Get only the files ". fmr" from a folder, I put an openFileDialog.Filter but do not know if it is correct to do this.

  • Take out this part: "All files (*.*)|*.*". I was trying to understand this method listaArquivos (maybe from that p/ delete the files with unwanted extensions for the code to get more robust) and it seems to me quite confused, not sure if it is doing what you really need. Even if it is, it’s not a very nice code. But that’s another problem. Perhaps it would be the case to ask a new question with this problem, otherwise the question becomes too wide.

  • I took and modified also for another type of file but it is still picking up everything.

  • I will do more research but I think you should ask another question about it to get organized. Detail this part well for everyone to understand. Put all relevant parts. For example, you haven’t put where the variable comes from openFileDialog.

  • All right thanks, I’ll ask another more detailed question and I’ll also organize the code, comment more on it and such. Thanks for the help.

Show 2 more comments

0

You can use the class FileInfo which provides properties and instance methods for creating, copying, deleting, scrolling and opening files.

List<string> selectedPath = listaArquivos(folderBrowserDialog.SelectedPath);
foreach (string s in selectedPath)
{
    FileInfo fileInfo = new FileInfo(s);
    grvShowFile.Rows.Add(fileInfo.Name);
}

Browser other questions tagged

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