Reputation: 21
Now, I know there are already a lot of questions on Stackoverflow about folder recursion and getting a folder including it's sub-directories etc. pp., but I haven't found anything related to what I'm encountering here.
My problem is as follows:
I've taken the code snippet about Folder Recursion from here (page bottom) and adapted it to my needs; that is, having it not write all (sub)directories to the console but having it add them to a list instead. Here is my code (note the part that's commented out):
private static List<String> ShowAllFoldersUnder(string path)
{
var folderList = new List<String>();
try
{
if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
!= FileAttributes.ReparsePoint)
{
foreach (string folder in Directory.GetDirectories(path))
{
folderList.Add(folder);
// Console.WriteLine(folder);
ShowAllFoldersUnder(folder);
}
}
}
catch (UnauthorizedAccessException) { }
return folderList;
}
This is how I call it (Dir
is a string
containing the path):
var _folders = ShowAllFoldersUnder(Dir);
foreach (string folder in _folders)
{
Console.WriteLine(folder);
}
The Problem is only the first level of folders is added to the list, meaning my output is for example:
[...]
C:\Users\Test\Pictures
C:\Users\Test\Recent
C:\Users\Test\Saved Games
C:\Users\Test\Searches
C:\Users\Test\SendTo
[...]
If I however uncomment Console.WriteLine(folder);
from the method, it echoes all (sub)directories to the console:
[...]
C:\Users\Test\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned
C:\Users\Test\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\ImplicitAppShortcuts
C:\Users\Test\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
C:\Users\Test\AppData\Roaming\Microsoft\Internet Explorer\UserData
C:\Users\Test\AppData\Roaming\Microsoft\Internet Explorer\UserData\Low
C:\Users\Test\AppData\Roaming\Microsoft\MMC
C:\Users\Test\AppData\Roaming\Microsoft\Network
[...]
I'm desperate after having spent hours researching what could be my mistake. Does anybody have a clue what's causing my problem?
Upvotes: 1
Views: 91
Reputation: 22651
The method ShowAllFoldersUnder
returns a list of strings, but the only time you're actually doing something with it is in the 'main' method, where you write them to the Console
.
You need to change
ShowAllFoldersUnder(folder);
to
folderList.AddRange(ShowAllFoldersUnder(folder));
Upvotes: 2
Reputation: 21568
It would appear that you are doing nothing with the folders found in the recursive calls to ShowAllFoldersUnder
.
This modification should resolve it. Change:
ShowAllFoldersUnder(folder);
to:
folderList.AddRange(ShowAllFoldersUnder(folder));
In production code I would likely refactor it to use a single List
throughout the recursion so as to avoid any overhead of creating and merging multiple lists.
Upvotes: 3
Reputation: 20764
modify your method to this
private static void ShowAllFoldersUnder(string path, List<string> folderList)
{
try
{
if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
!= FileAttributes.ReparsePoint)
{
foreach (string folder in Directory.GetDirectories(path))
{
folderList.Add(folder);
// Console.WriteLine(folder);
ShowAllFoldersUnder(folder, folderList);
}
}
}
catch (UnauthorizedAccessException) { }
}
now call it like this
var _folders = new List<string>();
ShowAllFoldersUnder(Dir, _folders);
this way you prevent many list creation and memory consumption in other answers. by using this way you supply an initial list to the method and it will add all the entries to it, but other answers will generate a list each time and then copy the result to the upper list and this will cause a lot of memory allocation, copy and deallocation.
Upvotes: 2