2009-03-23 3 views
8

私はファイルシステムで作業していますが、リスト<>のプロパティファイルパスを持つファイルオブジェクトがあります。ファイルシステムツリービュー

....

C:/WINDOWS/Temp/ErrorLog.txt 
C:/Program Files/FileZilla/GPL.html 
C:/Documents and Settings/Administrator/ntuser.dat.LOG 

など:基本的に私は、.NETでツリービューを作成する必要がありますが、イムは、私のようなリストからツリー構造を作成する必要があるとして、これをやって行くための最善の方法を考えるのに苦労します

リストは構造化されていないため、現在のオブジェクト構造を変更することはできません。

私はC#で作業しています。

寄付をいただきありがとうございました

+0

GetOrCreateTreeNode方法において(http://stackoverflow.com/questions/1155977/populate-treeview-from-a-list-of-path) –

答えて

16

あなたはこのような何かがうまくいくの文字列に固執したい場合は...

TreeNode root = new TreeNode(); 
TreeNode node = root; 
treeView1.Nodes.Add(root); 

foreach (string filePath in myList) // myList is your list of paths 
{ 
    node = root; 
    foreach (string pathBits in filePath.Split('/')) 
    { 
     node = AddNode(node, pathBits); 
    } 
} 


private TreeNode AddNode(TreeNode node, string key) 
{ 
    if (node.Nodes.ContainsKey(key)) 
    { 
     return node.Nodes[key]; 
    } 
    else 
    { 
     return node.Nodes.Add(key, key); 
    } 
} 
+0

これはb-e-a-utifulです!どうもありがとうございました! – user31849

+0

Webで動かないのですか? –

+0

素晴らしいです。まさに私が必要としたもの。私が必要とした唯一の変更は、 'filePath.Split( '/')'を 'filePath.Split( '/'、StringSplitOptions.RemoveEmptyEntries)'に変更することでした。ありがとうございました! –

4

文字列をFileInfoに変換します。

オブジェクトが作成されたら、ディレクトリプロパティを使用して、各パスのDirectoryInfoを取得できます。FileInfo

あなたがパスのDirectoryInfoを持っていたら、それは+ファイル名、ディレクトリのリストに各パスを回すためにDirectoryInfoにおける親の参照を「歩く」するのは簡単です - すなわち:

{"C:","Windows","Temp","ErrorLog.txt"} 

これはかなりする必要がありますツリービューに簡単に挿入できます。順番にパスの各セクションを探してください。存在しない場合は、追加してください。

2

再帰を試してみてください。

private void AddFiles() 
{ 
    // Iterate your list with FileInfos here 
    foreach(var fileInfo in new Collection<FileInfo>()) 
    { 
    GetOrCreateTreeNode(fileInfo.Directory).Nodes.Add(new TreeNode(fileInfo.Name)); 
    } 
} 

private TreeNode GetOrCreateTreeNode(DirectoryInfo directory) 
{ 
    if(directory.Parent == null) 
    { 
    // Access your TreeView control here: 
    var rootNode = <TreeView>.Nodes[directory.Name]; 
    if(rootNode == null) 
    { 
     rootNode = new TreeNode(directory.Name); 
     // Access your TreeView control here: 
     <TreeView>.Nodes.Add(rootNode); 
    } 
    return rootNode; 
    } 

    var parent = GetOrCreateTreeNode(directory.Parent); 
    var node = parent.Nodes[directory.Name]; 
    if(node == null) 
    { 
    node = new DirectoryNode(directory); 
    parent.Nodes.Add(node); 
    } 
    return node; 
} 

このコードは、あなただけのアイデアを与える必要があります - 私はここに投稿する前にそれをテストしていないことを認めざるを得ません。

+0

[パスのリストからツリービューを取り込む]の可能な重複、Directories.Nodes [directory.Name]行は何の意味ですか?ディレクトリのようなものは存在しないので...これまでのあなたの助けは非常に高く評価されています! – user31849

+0

訂正を参照してください - これは要素です – tanascius

+0

ありがとうございます、それはそうだと思っていましたが、私は確信していました – user31849

2
private void Form1_Load(object sender, EventArgs e) 
    { 
     var paths = new List<string> 
         { 
          @"C:\WINDOWS\AppPatch\MUI\040C", 
          @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727", 
          @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI", 
          @"C:\WINDOWS\addins", 
          @"C:\WINDOWS\AppPatch", 
          @"C:\WINDOWS\AppPatch\MUI", 
          @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409" 
         }; 
     treeView1.PathSeparator = @"\"; 
     PopulateTreeView(treeView1, paths, '\\'); 
    } 

    private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator) 
    { 
     TreeNode lastNode = null; 
     string subPathAgg; 
     foreach (string path in paths) 
     { 
      subPathAgg = string.Empty; 
      foreach (string subPath in path.Split(pathSeparator)) 
      { 
       subPathAgg += subPath + pathSeparator; 
       TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true); 
       if (nodes.Length == 0) 
        if (lastNode == null) 
         lastNode = treeView.Nodes.Add(subPathAgg, subPath); 
        else 
         lastNode = lastNode.Nodes.Add(subPathAgg, subPath); 
       else 
        lastNode = nodes[0]; 
      } 
     } 
    } 

alt text

+0

-1ポールの解決策が働いたいくつかの状況で、間違った親を見つけました。 –

0

EHoscaの作品は、1回の変更で、完全に私のために働いた - 私は設定する必要がありましたパス領域のforeachパスの後には何もしません。

これは上記のeHoscaのコードで、VBに移植されています。

Private Sub PopulateTreeView(tv As TreeView, paths As List(Of String), pathSeparator As Char) 
    Dim lastnode As TreeNode = Nothing 
    Dim subPathAgg As String 
    For Each path In paths 
     subPathAgg = String.Empty 
     lastnode = Nothing 
     For Each subPath In path.Split(pathSeparator) 
      subPathAgg += subPath + pathSeparator 
      Dim nodes() As TreeNode = tv.Nodes.Find(subPathAgg, True) 
      If nodes.Length = 0 Then 
       If IsNothing(lastnode) Then 
        lastnode = tv.Nodes.Add(subPathAgg, subPath) 
       Else 
        lastnode = lastnode.Nodes.Add(subPathAgg, subPath) 
       End If 
      Else 
       lastnode = nodes(0) 
      End If 
     Next 
    Next 
End Sub