Reputation: 15
I have a listview with folders. I am trying to get the files of a selected folder from the listview to add them to a listbox.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
string filepath = Path.GetDirectoryName(listView1.SelectedItems);
listBox1.Items.Add(filepath);
}
This is what I have and I know this should only add the folder to the listbox but it just adds (Collection) to the listbox.
Any help would be appreciated.
EDIT: Following @aria's code I switch Path.GetDirectoryName(listview1.SelectedItems[i].Text)
to Directory.GetFiles[i].Text)
but now it gives me an error.
System.IO.DirectoryNotFoundException: 'Could not find a part of the path
and the path is to the debug folder of the project, not the actual folder path.
Why is it going to the debug folder instead?
Upvotes: 0
Views: 1231
Reputation: 3844
As their comments mentions SelectedItems
will give you collection of ListViewItem
so you can iterate over them like.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count == 0) return;
for(int i=0;i<=listView1.SelectedItems.Count-1;i++)
{
string filepath = Path.GetDirectoryName(listView1.SelectedItems[i].Text);
listBox1.Items.Add(filepath);
}
}
Upvotes: 1