How to pass names of a file to a DataGridView using FolderBrowserDialog?

3

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

So far I've only been able to get him to get the file's path and move on to DataGridView and now I want to make him pass the name too.

Here's the code part for 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 can also tell me how I can get it to just find the files that are in the filter, I would appreciate it.

    
asked by anonymous 15.12.2014 / 15:28

2 answers

1

To get only the file name contained in a path, the Path.GetFileName() .

It will probably do something like this:

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

I placed GitHub for future reference .

    
15.12.2014 / 15:40
0

You can use the FileInfo that provides instance properties and methods for creating, copying, deleting, moving, and opening files.

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