I have a listview and want to show folders in C drive in listview. but when I clicked double to listview, it doesn't show folders in folder, for example; folders in program files. I wonder how can I list folders in C drive by mouse duble clicking?
private void lstw_MouseDoubleClick(object sender, MouseEventArgs e)
{
for (int i = 0; i < lstw.Items.Count; i++)
{
if (lstw.Items[i].Selected == true)
{
string path = lstw.Items[i].Name;
lstw.Items.Clear();
listFill(path);
}
}
}
private void listFill(string pth)
{
DirectoryInfo dInfo = new DirectoryInfo(pth);
try
{
foreach (DirectoryInfo di in dInfo.GetDirectories())
{
lstw.Items.Add(di.Name,di.FullName);
}
}
catch { }
}
{
for (int i = 0; i < lstw.Items.Count; i++)
{
if (lstw.Items[i].Selected == true)
{
string path = lstw.Items[i].Name;
lstw.Items.Clear();
listFill(path);
}
}
}
private void listFill(string pth)
{
DirectoryInfo dInfo = new DirectoryInfo(pth);
try
{
foreach (DirectoryInfo di in dInfo.GetDirectories())
{
lstw.Items.Add(di.Name,di.FullName);
}
}
catch { }
}
VulpesPosted Dec 5, 2014, 1:12 PM
The original exception you were getting suggested that the path itself was invalid. So am I right in thinking that the selected ListViewItem contains the text: c:\program files ?
One thing I would do is to place 'break' after you've refilled the ListView since the results of continuing to iterate it could be unpredictable:
Serhan SrhnPosted Dec 3, 2014, 9:19 PM
but the result is same. it didn't give an error, but didn't list. listview seems no item.
VulpesPosted Dec 3, 2014, 5:48 PM
When I tried it myself, this is what happened to me on one of the SQL Server files.
However, I've found a solution on the following site which is to do the recursion yourself on the sub-folders. This also allows you to deal with another possible problem - reparse points - at the same time:
http://www.blackwasp.co.uk/FolderRecursion.aspx
I've adapted the code given on that site to the situation here and so I'd now try this:
private void listFill(string path)
{
try
{
if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
!= FileAttributes.ReparsePoint)
{
foreach (string folder in Directory.GetDirectories(path))
{
lstw.Items.Add(Path.GetFileName(folder), folder);
listFill(folder);
}
}
}
catch { } // leave this to catch any exception
}
Serhan SrhnPosted Dec 3, 2014, 2:39 PM
it didn't gave an error but it didn't list. I clicked to list on program files folder, listview looks no item.
VulpesPosted Dec 3, 2014, 10:11 AM
DirectoryInfo dInfo = new DirectoryInfo(pth);
rather than the one we've altered.
This may be because in this line:
string path = lstw.Items[i].Name;
you're using the Name property of the ListViewItem (which defaults to an empty string) rather than the Text property?
However, you could move the offending line into the 'try' block so any such exceptions will be caught and swallowed.
Serhan SrhnPosted Dec 3, 2014, 9:30 AM
VulpesPosted Dec 3, 2014, 9:13 AM