浏览Web表单中的目录 [英] Browse a directory in a web form

查看:93
本文介绍了浏览Web表单中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要使用ASP.NET浏览webform中的目录。

我设法获取所选文件夹的所有子目录/目录,但问题是我不知道每个目录有多少级别的子目录。例如,如果我浏览一个名为AA的文件夹/目录,它有50个子文件夹,每个子文件夹可以有另一个子文件夹和另一个子文件夹,或者只是一个文件列表。



有没有办法向用户显示目录组件,这样他就可以导航,直到找到他要查找的文件,无论该文件夹有多少子文件夹?



任何帮助都会非常感激,因为我没有想法

提前致谢



我是什么尝试过:



这是我浏览/列出所选目录的所有子目录的方式:

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

推荐答案

\ abc\efg\xyz\qwer \ install_guides);
files = dir.GetFiles();
directories = dir.GetDirectories();

foreach(目录中的DirectoryInfo d)
{
sb.Append( < a href = \http:// abc / install_guides /+ d.Name.ToString()+\>);
sb.Append(d.Name.ToString() +< / a>< br />);
}
\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 />"); }


我设法使用TreeView。我创建了两个函数,一个用于填充TreeView,如果有子目录,另一个用于填充TreeView,如果没有文件夹。

第一个:

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



希望它会帮助别人



谢谢,

Samira


Hope It's gonna help others

Thanks,
Samira


这篇关于浏览Web表单中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆