build a menu from file structure

169 views Asked by At

I am building a menu depending upon the file structure on server. So, this is the file structure:

Videos
    Reports 
        Customers
    SetUp
        Customers
        Products

So, i wrote this code

System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/Videos"));
RadMenuItem RootNode = OutputDirectory(RootDir, null);
videoMenu.Items.Add(RootNode);

In output directory I'll iterate through them and return them. So, now i wanted output which looks like this:

Reports 
    Customers
SetUp
    Customers
    Products

I dont want videos to be the parent level. INstead wanted Reports and Setup to be at top. can you please help me.

2

There are 2 answers

0
jdmcnair On

Sounds like you'll want to add several parent level nodes to your RadMenu (Reports, Setup, etc.), rather than just adding the single RootNode. Whatever your OutputDirectory method is doing, make it return the children of "Videos" as an IEnumerable of nodes, and add those rather than RootNode.

0
Magnus On

You probably need a recursive function. Something like this:

void Main()
{   
    var dirs = new DirectoryInfo(Server.MapPath("~/Videos")).GetDirectories();
    CreateTree(videoMenu.Items, dirs):
}

public void CreateTree(IList<RadMenuItem> parantCollection, IEnumerable<DirectoryInfo> parentDirs)
{
    foreach (var dir in parentDirs)
    {
        var node = OutputDirectory(dir, null);
        parantCollection.Add(node);
        CreateTree(node.Items, dir.GetDirectories());
    }
}