如何在asp.net 4.5中浏览文件夹 [英] How to browse folders in asp.net 4.5

查看:100
本文介绍了如何在asp.net 4.5中浏览文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,如何在Asp.net 4.5中浏览文件夹而不是浏览文件。仅供参考,我的要求是我应该能够选择一个文件夹而且数量不行。文件夹和子文件夹中的文件。但不幸的是我找不到浏览文件夹的机制。



提前谢谢。

Hi Everyone, How can I browse folders rather than browsing files in Asp.net 4.5. FYI, my requirement is something like I should be able to pick a folder and count no. of files in folder and subfolders. But unfortunately I couldn't find the mechanism to browse for folders.

Thanks in advance.

推荐答案

递归



使用递归扫描目录 [ ^ ]


感谢您的关注,



首先,我首先尝试使用FileUpload控件选择文件夹,但是我们只能选择文件而不能选择任何文件夹。



我使用TreeView控件来填充从我的电脑开始的目录作为默认启动当前用户的-up路径。但是我无法获得Windows 8的计算机(这个PC)路径。



Thank you for your concern,

At first, I've tried at first to select folder by using FileUpload control, but we can only choose files but not any folder through that.

And I've used TreeView control to populate the directories starting from the "My Computer" as default start-up path of the current user. But I couldn't get the my computer (this pc) path of Windows 8.

protected void Page_Load(object sender, EventArgs e)
    {
        //set the treeview's first node to our Images folder

        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);

        TreeView1.Nodes[0].Value = path;
    }

    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (IsCallback)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                DirectoryInfo directory = null;
                directory = new DirectoryInfo(e.Node.Value);

                foreach (DirectoryInfo subtree in directory.GetDirectories())
                {
                    TreeNode subNode = new TreeNode(subtree.Name);
                    subNode.Value = subtree.FullName;
                    try
                    {
                        if (subtree.GetDirectories().Length > 0 | subtree.GetFiles().Length > 0)
                        {
                            subNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                            subNode.PopulateOnDemand = true;
                            subNode.NavigateUrl = "#";
                        }
                    }
                    catch
                    {

                    }
                    e.Node.ChildNodes.Add(subNode);
                }
                foreach (FileInfo fi in directory.GetFiles())
                {
                    TreeNode subNode = new TreeNode(fi.Name);
                    e.Node.ChildNodes.Add(subNode);
                    subNode.NavigateUrl = "Images/" + fi.Name.ToString();
                }
            }
        }
    }


这篇关于如何在asp.net 4.5中浏览文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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