0

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.

4

3 回答 3

4
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]);
}

试试这个,并获取所有文件夹名称dirList并将其添加到下拉列表中。

于 2013-10-26T08:54:44.090 回答
1

如果您使用 inbuild Directory.EnumerateDirectories 方法会更容易。 http://msdn.microsoft.com/de-de/library/vstudio/dd383462(v=vs.110).aspx

你只是这样称呼它:

Directory.EnumerateDirectories("rootpath", "*", SearchOption.AllDirectories);

这将为您提供所有目录(“*”)的 IEnumerable 及其完整路径。枚举 SearchOption.AllDirectories 表示您希望搜索是递归的。根路径是您的起点。

另外,您可以通过像这样扩展第二个参数来过滤此方法:

Directory.EnumerateDirectories("rootpath", "test*", SearchOption.AllDirectories);

这将导致所有以 test ("test*") 开头的目录的 IEnumerable

编辑:

您应该考虑显示相对路径,而不是仅显示可能导致重复列表条目的名称。

var directories = (from x in Directory.EnumerateDirectories("rootpath", "*", SearchOption.AllDirectories)
                    select x.SubString("rootpath".Length)).ToList();
于 2013-10-26T08:57:47.857 回答
0

使用以下方法获取所有文件夹的列表:

 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);
            }
        }
    }
}

一旦你得到目录和子目录的列表,就用下拉菜单绑定它。使用以下代码调用此方法

 DirectoryList directoryList = new DirectoryList();

    //output pram
    ArrayList<String> dirList = new ArrayList<String>();

    directoryList.traverse(new File("D:\\Workspace\\Netbeans\\addapp"), dirList);
于 2013-10-26T08:50:19.067 回答