Reputation: 878
I have code to find and display name of all folder in a rootfolder:
private string[] GetFolderNames(string virtualDirPath)
{
string[] Directories;
if (Directory.Exists(virtualDirPath))
{
Directories = Directory.GetDirectories(virtualDirPath);
for (int i = 0; i < Directories.Length; i++)
{
Directories[i] = MapUrl(Directories[i]);//map path to the folder
}
return Directories;
}
else
{
return null;
}
}
And code bind data to Dropdownlist:
string[] folders = GetFolderNames(RootPath);
if (folders != null)
{
dropDownListFolders.DataSource = folders;
dropDownListFolders.DataBind();
}
else
{
dropDownListFolders.Items.Insert(0, "No folders available..");
}
As the code above, the Dropdownlist display all the folder name in the Rootfolder
with path= virtualDirPath
;
But I wonder if in every child folder still has some subfolder, and in each subfolder has some more subfolder and so on more and more, so that how can I get all the name of that subfolders.
Try to make more for
loop inside the first one, but it really mess me up. And it seems that is not the good way.
I need the Dropdownlist display all the subfolder name, child of sudfolder and child of child folder... in the rootfolder. Help! I need your opinion to find a better way to to it.
Upvotes: 0
Views: 5372
Reputation: 7440
Its easier if you use the inbuild Directory.EnumerateDirectories method. http://msdn.microsoft.com/de-de/library/vstudio/dd383462(v=vs.110).aspx
You just call it like this:
Directory.EnumerateDirectories("rootpath", "*", SearchOption.AllDirectories);
This will give you an IEnumerable of the all directories ("*") with its full path. The enum SearchOption.AllDirectories is saying you want your search to be recursive. The rootpath is your starting point.
Additional you could filter this method by extending the second paramater like this:
Directory.EnumerateDirectories("rootpath", "test*", SearchOption.AllDirectories);
This will result in a IEnumerable of all directories starting with test ("test*")
Edit:
You should consider displaying relative paths, instead of only names which can result in duplicate list entries.
var directories = (from x in Directory.EnumerateDirectories("rootpath", "*", SearchOption.AllDirectories)
select x.SubString("rootpath".Length)).ToList();
Upvotes: 1
Reputation: 2328
List<string> dirList = new List<string>();
DirectoryInfo[] dir = new DirectoryInfo(@"D:\Path").GetDirectories("*.*", SearchOption.AllDirectories);
foreach(DirectoryInfo d in dir)
{
dirList.Add(d.Name);
}
for (int i = 0; i < dirList.Count; i++)
{
Console.WriteLine(dirList[i]);
}
Try this, and get all the folder names in dirList
and add it into the dropdown.
Upvotes: 4
Reputation: 1315
Use following method to get list of All the folders:
public void traverse(File file, List<String> dirList) {
// Print the name of the entry
System.out.println(file);
// Check if it is a directory
if (file.isDirectory()) {
System.out.println(file);
//add directory name in the list
dirList.add(file.getName());
// Get a list of all the entries in the directory
String entries[] = file.list();
// Ensure that the list is not null
if (entries != null) {
// Loop over all the entries
for (String entry : entries) {
// Recursive call to traverse
traverse(new File(file, entry),dirList);
}
}
}
}
once you get the list of directory and sub directories bind it with dropdown. Use following code to call this method
DirectoryList directoryList = new DirectoryList();
//output pram
ArrayList<String> dirList = new ArrayList<String>();
directoryList.traverse(new File("D:\\Workspace\\Netbeans\\addapp"), dirList);
Upvotes: 0