简体   繁体   English

在 ListView 中显示文件夹和文件

[英]Display Folders and Files in ListView

I created a function in a Windows Form Application that allows users to view a folder structure (TreeForm) and the files within the folders in a ListView.我在 Windows 窗体应用程序中创建了一个函数,该函数允许用户在 ListView 中查看文件夹结构 (TreeForm) 和文件夹内的文件。 Now, I need to create the same function for a WebForm application.现在,我需要为 WebForm 应用程序创建相同的函数。 I attempted to use the same code but have found that the asp.net webform controls do not contain the same properties as the winform.我尝试使用相同的代码,但发现 asp.net webform 控件不包含与 winform 相同的属性。 Below is part of the code which I cannot determine how to convert so that it maybe used with on a webform page.下面是我无法确定如何转换的代码的一部分,以便它可以在网络表单页面上使用。 Does anyone now how to convert the following code so that it can be used with an asp.net webform?有没有人现在如何转换以下代码,以便它可以与 asp.net webform 一起使用? Any assistance would be greatly appreciated.任何帮助将不胜感激。

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {

        ListView1.Items.Clear();
        ListViewItem.ListViewSubItem[] subItems;
        List<string> permittedFoldersFiles = new List<string>();

        if (permittedFoldersFiles.Contains(dir.Name))
        {
            item = new ListViewItem(dir.Name, 0);
            subItems = new ListViewItem.ListViewSubItem[]
                    {new ListViewItem.ListViewSubItem(item, "Directory"), 
                    new ListViewItem.ListViewSubItem(item, 
                    dir.LastAccessTime.ToShortDateString())};
            item.SubItems.AddRange(subItems);
            listView1.Items.Add(item);
        }
    }

Try use this sample it's work very well尝试使用此示例效果很好

protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DirectoryInfo rootInfo = new DirectoryInfo(Server.MapPath("~/MyFolder/"));
                this.PopulateTreeView(rootInfo, null);
            }
        }

        private void PopulateTreeView(DirectoryInfo dirInfo, 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.
                    TreeView1.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,
                        Target = "_blank",
                        NavigateUrl = (new Uri(Server.MapPath("~/"))).MakeRelativeUri(new Uri(file.FullName)).ToString()
                    };
                    directoryNode.ChildNodes.Add(fileNode);
                }

                PopulateTreeView(directory, directoryNode);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM