I currently have a method that lists all sub-directories and I think I need to supplement it for another method.
private static List<string> GetDirectories(string directory, string searchPattern)
{
try
{
return Directory.GetDirectories(directory, searchPattern).ToList();
}
catch (UnauthorizedAccessException)
{
return new List<string>();
}
}
I then call it like this:
var directories = GetDirectories(directory, fileExtension);
I can list all sub-directories on the next level but not the level inside of it. The catch is my code won't exit if there's a folder I don't have access to.
e.g. when I pass "C:\" and "*.*" I can get
C:\Folder1 C:\Folder2 C:\Folder3
but not the folders inside of it.
I am trying to make a List
that would make it so that if I pass C:\\
and \*.xls
, I'll be able to get the result below as a List
:
Directory | File Count C:\Folder1 | 3 (3 files under \Folder with and xls extension) C:\Folder\Sub | 2 C:\Folder2 | 5
and so on.
Thank you in advance.