Reputation: 1804
I want to have list of all folders and files for x depth.
If x is 2 than I will have information about all folders and files from first folder, and all folders and files from folders in first folder.
How to do this?
Upvotes: 2
Views: 6115
Reputation: 5566
if you just want the filenames you can do that with a recursive method. something like that: (no garantee for total correctness, i write without compiler)
public static List<String> GetSubfoldersAndFiles(DirectoryInfo di, int deep)
{
List<string> myContent = new List<string>();
foreach(FileInfo fi in di.GetFiles())
{
myContent.Add(fi.FullName);
}
if(deep > 0)
{
foreach(DirectoryInfo subDi in di.GetDirectories())
{
myContent.AddRange(GetSubfoldersAndFiles(subDi, deep - 1).ToArray());
}
}
return myContent;
}
and the call would be like that:
List<string> content = GetSubfoldersAndFiles(new DirectoryInfo(@"c:\"), 3);
Upvotes: 2
Reputation: 11
Extending @pstrjds answer
private static IReadOnlyList<string> SearchFiles(string root, string pattern = "*", int depth = 1, params string[] exceptFolders)
{
var files = Directory
.EnumerateDirectories(root)
.Where(it => !exceptFolders.Contains(Path.GetRelativePath(root, it)) && depth > 0)
.SelectMany(it => SearchFiles(it, pattern, depth - 1, exceptFolders))
.ToList();
files.AddRange(Directory.EnumerateFiles(root, pattern));
return files;
}
Upvotes: 1
Reputation: 90
All subfolders
public List<string> listDir(string pPath)
{
List<string> dirList = new List<string>();
listDir(pPath, dirList);
return dirList;
}
private void listDir(string pPath, List<string> pList)
{
string[] loDirs = System.IO.Directory.GetDirectories(pPath);
foreach (string loDir in loDirs)
{
pList.Add(loDir);
listDir(loDir, pList);
}
}
Upvotes: 1
Reputation: 17428
This code will do what other answers are doing, but also return the folder names (as that appears to be part of what you are asking). This will require .Net 4.0. If you wish to keep track of which are folders and which are files you could return a tuple containing a list of files and a list of folders.
List<string> GetFilesAndFolders(string root, int depth)
{
var list = new List<string>();
foreach(var directory in Directory.EnumerateDirectories(root))
{
list.Add(directory);
if (depth > 0)
{
list.AddRange(GetFilesAndFolders(directory, depth-1));
}
}
list.AddRange(Directory.EnumerateFiles(root));
return list;
}
EDIT: Code that keeps the folders and files separate
Tuple<List<string>,List<string>> GetFilesAndFolders(string root, int depth)
{
var folders = new List<string>();
var files = new List<string>();
foreach(var directory in Directory.EnumerateDirectories(root))
{
folders.Add(directory);
if (depth > 0)
{
var result = GetFilesAndFolders(directory, depth-1);
folders.AddRange(result.Item1);
files.AddRange(result.Item2);
}
}
files.AddRange(Directory.EnumerateFiles(root));
return new Tuple<List<string>,List<string>>(folders, files);
}
Upvotes: 5
Reputation: 24316
I feel as though this has been solved before. Basically what you want to do is ask the current File if it is a directory, if true and x has not maxed out yet grab that File's SubDirectories. Also, you need to check if there are files present in that directory and retrieve them as well.
Upvotes: 2