Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm scanning through a directory and want to display the sub-directories in list view. Surprisingly I can only see some of the directories, others are not shown. I tried debugging it and it scans through all sub-directories, but just don't display all. Could you help me figure out the problem? I guess I'm doing something wrong with the use of listview.

Here is my code.

C#
foreach (DirectoryInfo di in dir.GetDirectories())
{
   ListViewItem item = new ListViewItem();
   item.ImageIndex = 0;
   item.Text = di.Name;
   item.Tag = di.FullName;
   listView_Files.Items.Add(item);
}


Thanks.
Posted
Updated 3-Jun-11 12:17pm
v2

Did you try using the SearchOptions parameter for GetDirectories()?

C#
DirectoryInfo dir = new DirectoryInfo("");
foreach (DirectoryInfo di in dir.GetDirectories("*.*", SearchOption.AllDirectories)
{
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Jun-11 0:36am    
Correct. Also can use Directory class, same option. My 5.
--SA
C#
DirectoryInfo di = new DirectoryInfo(@"C:\Windows");
DirectoryInfo[] _allDirectories = di.GetDirectori("*.*",SearchOption.AllDirectories)
foreach (var item in _allDirectories)
{
   ListViewItem item = new ListViewItem();
   item.ImageIndex = 0;
   item.Text = di.Name;
   item.Tag = di.FullName;
   listView_Files.Items.Add(item);
}
 
Share this answer
 
I tried the above options but didn't help. I wanted to display the directories of the top level only. I also tried using the below which didn't solve either.

DirectoryInfo[] _allDirectories = dir.GetDirectories("*.*", SearchOption.TopDirectoryOnly);

I am not able to figure out why some of the directories are skipped. Are there any diectory settings used by the listview? I see no difference in properties of directory which are skipped and the ones shown :(
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900