2015-10-15 76 views
0

我有一個可觀察的路徑集合。 我想要做的事情是更新我的treeView更改集合。 你能否幫我創建一個方法,該方法將Treeview,FilePath和PathSeparator作爲參數,並將新節點添加到我的treeView中。這是我現在有:使用新文件路徑更新TreeView

private void MyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
{ 
    TreeViewAddNode(TreeView,Path,PathSeparator) 
} 

TreeViewAddNode(TreeView treeView, string path, char pathSeparator) 
{ 
    foreach (string subPath in path.Split(pathSeparator)) 
    { 
     //Hear should be logic to add new nodes if they don't exist } 
    } 
} 

正如我想的結果有類似的東西:
C:
--Temp
---- FILE1.TXT
----文件2。 TXT
----新Foledr
------- File3.txt
--AnotherFolder
---- File4.txt
d:
- 新建文件夾
---- FILE.TXT

+0

什麼是香草'treeView.Nodes.Add(path);'? –

+0

這將是一個單獨的節點,我需要類似的東西: C: ---溫度 ------ FILE.TXT --- Program Files文件 ------ SmthElse – Ivan

+0

所以應該是類似的東西: 的foreach(字符串子路徑在path.Split(pathSeparator)){ // 聽見應該是邏輯添加新的節點,如果不存在 他們} – Ivan

回答

1

編輯

現在有了更好的理解上被問:

private void TreeViewAddNode(TreeView treeView, string path, char pathSeparator) 
{ 
    string[] split = path.Split(pathSeparator); 

    for(int i = 0; i < split.Length; i++) 
    { 
     if(i == 0) 
     { 
      checkTreeView(treeView, split[0]); 
     } 
     else 
     { 
      TreeNode node = treeView1.Nodes.Find(split[i - 1], true)[0]; 
      checkNodes(node, split[i]);           
     } 
    }     
} 

private void checkTreeView(TreeView treeView, string path) 
{ 
    bool exists = false; 

    foreach(TreeNode node in treeView.Nodes) 
    { 
     if(node.Text == path) 
     { 
      exists = true; 
     } 
    } 

    if(!exists) 
    { 
     TreeNode node = treeView.Nodes.Add(path); 
     node.Name = path; 
    } 
} 

private void checkNodes(TreeNode parent, string path) 
{ 
    bool exists = false; 

    foreach(TreeNode node in parent.Nodes) 
    { 
     if(node.Text == path) 
     { 
      exists = true; 
     }     
    } 

    if(!exists) 
    { 
     TreeNode node = parent.Nodes.Add(path); 
     node.Name = path; 
    } 
} 

checkTreeView檢查的路徑是在TreeView目前媒體鏈接節點。如果它沒有添加到樹視圖。 checkNodes也一樣。

+0

不是真的,你的版本不顯示文件夾的層次結構,它們都在一個層次上 – Ivan

+0

然後,您可能希望在您的問題中指定您希望底層文件夾作爲子節點。 – Huntt

+0

對不起,我編輯了我的問題 – Ivan