3

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
learningdb
  • 169
  • 1
  • 2
  • 8
  • Can you provide an example of the result you want to achieve? – michele Jan 27 '11 at 10:39
  • Not 100% sure what you want, do you just want to recursively get all folders and files? – gideon Jan 27 '11 at 10:39
  • yes once i get the dirlist, I want to loop through each dir and get a list of nested subdirs and finally file in that subdir – learningdb Jan 27 '11 at 10:40
  • @lea do `dirlist` and `filelist` return ALL folders and files including in sub folders of the root? Do they both return the full path? – Shadow The GPT Wizard Jan 27 '11 at 10:40
  • no that's the issue..dirlist just returns the directory names. Somehow i have to get the subdirs from these names – learningdb Jan 27 '11 at 10:43
  • To skip security exceptions: http://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure – digEmAll Jan 27 '11 at 10:46

2 Answers2

9

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));
Daniel T.
  • 37,212
  • 36
  • 139
  • 206
  • yes what do I give in the path..that's the issue. Actually the API they have given me is pretty screwed up. – learningdb Jan 27 '11 at 10:47
  • Whoops, changed `dirList.Select` to `dirList.SelectMany`. – Daniel T. Jan 27 '11 at 10:52
  • will this code also get directories inside subdirs? I mean it can nested to any level. So for eg: a subdir may contain some files and some more dirs which may contains files and dirs and so on. I want to list them all. – learningdb Jan 27 '11 at 10:52
  • Yes, it will recursively go down and get all files/directories from all sub-directories, no matter how deep. – Daniel T. Jan 27 '11 at 10:54
  • ok thanks one last ques..i have updated the question to show a preview of the output i wanted. How can i get the output..should i store the result in a list or print on console. Any ideas how to do either of them – learningdb Jan 27 '11 at 11:01
  • Unfortunately, getting a list of all the files in a directory and building out a tree like that are two different issues. Try looking up some other SO questions, like this one: http://stackoverflow.com/questions/1005551/construct-a-tree-structure-from-list-of-string-paths – Daniel T. Jan 27 '11 at 11:09
0

Have a look at the Directory Class

foreach(string dir in Directory.GetDirectories("c:\","",SearchOption.AllDirectories))
{
 Console.Writeline(dir);
 foreach(string file in Directory.GetFiles(dir))
 {
   Console.Writline(file);
 } 
}

This will print all directories under C:\ and then all the files in each of those directories. Does that help?

gideon
  • 19,329
  • 11
  • 72
  • 113