问题
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?
回答1:
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);
}
回答2:
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.
回答3:
OK, here is a simple solution.
private static IList<string> GetFilesToDepth(string path, int depth)
{
var files = Directory.EnumerateFiles(path).ToList();
if (depth > 0)
{
var folders = Directory.EnumerateDirectories(path);
foreach (var folder in folders)
{
files.AddRange(GetFiles(folder, depth - 1));
}
}
return files;
}
It might be attractive to change this into an EnumFilesToDepth(), ie to get rid of the ToList(). That would require a little more thought and work though, a recursive Iterator is tricky.
回答4:
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));
回答5:
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);
}
}
来源:https://stackoverflow.com/questions/6141648/list-of-all-folders-and-files-for-x-depth