簡體   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