繁体   English   中英

在列表框中显示树视图中所选目录的文件名

[英]Show file names in listbox for selected directory in treeview

我有一个按钮“选择文件夹”。 当我选择文件夹时,它将在treeview中显示所有目录和子目录。 一切正常。 我现在需要的是-当我在树形目录中的某个目录中单击时,在列表框中显示该目录中的所有文件。

private void button1_Click(object sender, EventArgs e)
{
    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
        if (directoryInfo.Exists)
        {
            VeidotKoku(directoryInfo, treeView1.Nodes);
        }
    }
}

private void VeidotKoku(DirectoryInfo directoryInfo, TreeNodeCollection Pievienot)
{
    TreeNode tagadejaNode = Pievienot.Add(directoryInfo.Name);

    foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
    {
        VeidotKoku(subdir, tagadejaNode.Nodes);
    }
}

System.IO.Path.GetFileNameWithoutExtension(fileLocationPath);

将获得无扩展路径。

否则,如果您只想获取值,则可以拆分路径并获取最后一个元素。

foreach(string filePath in filePaths)
{
    string[] brokenPath = filePath.Split('/');
    listBox.Add(brokenPath.Last());
}

我头顶

暂无
暂无

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

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