I have a custom object that contains methods that returns all directory and file names in a root
string[] dirlist = obj.GetDirectories();
//which returns all dir names in root
string[] filelist = obj.GetFiles();
//which return all file names in root
I cannot modify these methods. Once I get the dirlist, how do i get a list of all subdirs in it as well as files in the subdirs, ignoring the security exceptions. It can be nested to multiple levels. Anything in .NET 4?
Update: string[] dirList can also be read as List dirlist. Please give a solution that uses the latest features of .NET
DirectoryOne
- SubDirOne
- SubDirTwo
- FileOne
- FileTwo
- SubDirThree
DirectoryTwo
DirectoryOne
There are already built-in .NET methods to do this:
// get all files in folder and sub-folders
Directory.GetFiles(path, "*", SearchOption.AllDirectories);
// get all sub-directories
Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
Somehow I get the feeling this isn't the solution you're looking for though.
Update: I think I may know what you're trying to ask, since you tagged it as LINQ. If you want to get a list of all sub-directories and sub-folders given a list of directories, you can use the following code:
// get all files given a collection of directories as a string array
dirList.SelectMany(x => Directory.GetFiles(x, "*", SearchOption.AllDirectories));
// get all sub-directories given a collection of directories as a string array
dirList.SelectMany(x => x.Directory.GetDirectories(x, "*", SearchOption.AllDirectories));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With