I am trying to get a list of all the folders in the c:\Users\UserName directory of my W7 machine.
The issue I have when I use Directory.GetDirectories()
or Directory.GetFiles()
is that I often get exceptions that "access to the path is denied". I have ran VS in administrator mode and had no luck. The reason why is explained in my C# - Cannot access all files
I would like to know how to get the names of all subfolders (regardless of whether there are any files in the folder or not) where the subfolders could be N deep (so, undetermined number of subfolders) and store them as a List. This way, via a foreach loop, I can check on a folder by folder basis against certain logic to ensure I have access and that I want access!
The following code only looks at 1 level deep:
private List<string> GetAllFolders()
{
DirectoryInfo directoryInfo = new DirectoryInfo(this.sourceFolder);
List<string> allFolders = new List<string>();
foreach (DirectoryInfo subDirectoryInfo in directoryInfo.GetDirectories())
{
//logic
allFolders.Add(subDirectoryInfo.FullName);
}
return allFolders;
}
Is there a pattern I can use for this, or does any one have an example or a suggestion on how to proceed?