Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,
I need to browse a directory in a webform using ASP.NET.
I managed to get all the sub-directories of the selected folder/directory, but the problem is I don't know how many levels of sub-directories I have for each directory. For example If I browse a folder/directory called AA that has 50 sub-folders and each sub-folder could have another sub-folder and another sub-folder or just a list of files.

Is there's a way to display the directory components to the user so he could navigate till he finds the files he's looking for regardless how many subfolders that folder has?

Any help will be so much appreciated as I'm out of ideas
Thanks in advance

What I have tried:

This is how i browse/ list all sub-directories of the selected directory:
DirectoryInfo dir;
            StringBuilder sb = new StringBuilder();
            FileInfo[] files;
            DirectoryInfo[] directories;
            dir = new DirectoryInfo(@"\\10.0.0.8\d$\abc\efg\xyz\qwer\install_guides");
            files = dir.GetFiles();
            directories = dir.GetDirectories();

            foreach (DirectoryInfo d in directories)
            {
                sb.Append("<a href=\"http://abc/install_guides/" + d.Name.ToString() + "\">");
                sb.Append(d.Name.ToString() + "</a><br />");
            }
Posted
Updated 15-May-17 7:34am
Comments
[no name] 12-May-17 18:29pm    
https://msdn.microsoft.com/en-us/library/ms143316.aspx

1 solution

I managed to do it using TreeView. I have created two functions, one to populate TreeView if there's sub-directories and the other to populate TreeView if there's no folders.
First one:
private void PopulateTreeView_WithDirectories(DirectoryInfo dirInfo,TreeView treeView, TreeNode treeNode)
        {
                foreach (DirectoryInfo directory in dirInfo.GetDirectories())
                {
                    TreeNode directoryNode = new TreeNode
                    {
                        Text = directory.Name,
                        Value = directory.FullName
                    };
                    if (treeNode == null)
                    {
                        //If Root Node, add to TreeView.
                        treeView.Nodes.Add(directoryNode);
                    }
                    else
                    {
                        //If Child Node, add to Parent Node.
                        treeNode.ChildNodes.Add(directoryNode);
                    }

                    //Get all files in the Directory.
                    foreach (FileInfo file in directory.GetFiles())
                    {
                        //Add each file as Child Node.
                        TreeNode fileNode = new TreeNode
                        {
                            Text = file.Name,
                            //Value = file.FullName,                        
                            Value = file.FullName,
                            Target = "_blank",
                            NavigateUrl = (new Uri(Server.MapPath("~/"))).MakeRelativeUri(new Uri(file.FullName).ToString()

                        };
                        directoryNode.ChildNodes.Add(fileNode);
                    }

                    directoryNode.CollapseAll();
                    PopulateTreeView_WithDirectories(directory, treeView, directoryNode);
                }           
        }

Second one:
private void PopulateTreeView_WithFiles(DirectoryInfo dirInfo, TreeView treeView, TreeNode treeNode)
        {
           DirectoryInfo directory=dirInfo;
            if(directory.Name !=null)
            {
            TreeNode directoryNode = new TreeNode
            {
                Text =directory.Name,
                Value = directory.FullName
            };
            if (treeNode == null)
            {
                //If Root Node, add to TreeView.
                treeView.Nodes.Add(directoryNode);
            }
            else
            {
                //If Child Node, add to Parent Node.
                treeNode.ChildNodes.Add(directoryNode);
            }

            //Get all files in the Directory.
            foreach (FileInfo file in directory.GetFiles())
            {
                //Add each file as Child Node.
                TreeNode fileNode = new TreeNode
                {
                    Text = file.Name,
                    //Value = file.FullName,                        
                    Value = file.FullName,
                    Target = "_blank",
                    NavigateUrl = (new Uri(Server.MapPath("~/"))).MakeRelativeUri(new Uri(file.FullName).ToString()

                };
                directoryNode.ChildNodes.Add(fileNode);
            }

            directoryNode.CollapseAll();
            PopulateTreeView_WithDirectories(directory, treeView, directoryNode);
            }
        } 

Hope It's gonna help others

Thanks,
Samira
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900