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
6
  • Can you provide an example of the result you want to achieve? Commented Jan 27, 2011 at 10:39
  • Not 100% sure what you want, do you just want to recursively get all folders and files? Commented Jan 27, 2011 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 Commented Jan 27, 2011 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? Commented Jan 27, 2011 at 10:40
  • no that's the issue..dirlist just returns the directory names. Somehow i have to get the subdirs from these names Commented Jan 27, 2011 at 10:43

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

6 Comments

yes what do I give in the path..that's the issue. Actually the API they have given me is pretty screwed up.
Whoops, changed dirList.Select to dirList.SelectMany.
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.
Yes, it will recursively go down and get all files/directories from all sub-directories, no matter how deep.
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
|
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.