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.
– Maniero
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.
– Rodolfo Olivieri
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.
– Maniero