2016-09-25 1 views
0

현재 파일 찾아보기 컨트롤 (https://github.com/gregyjames/FileBrowser)에서 작업하고 있지만 코드에 성능 문제가 있습니다. 현재 here을 볼 수 있듯이 루트 디렉토리의 모든 파일을 트리보기로로드하는 두 개의 순환 루프가 있습니다. 처음에 루트 디렉토리에 하위 폴더를로드 한 다음 사용자 선택시 각 디렉토리의 하위 디렉토리를로드하려면 어떻게해야합니까 (예 : 사용자가 폴더를 선택한 다음 내용을로드). 어떤 도움을 주셔서 감사합니다!디렉토리 트리보기에서 지연 파일 하위/파일로드

답변

1

이 내가 자식 노드

// Form1.OnLoad 
protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 

    var root = new FolderFileNode(_path); 
    treeView1.Nodes.Add(root); 
    root.LoadNodes(); 

    treeView1.BeforeSelect += (sender, args) => 
    {    
     //This flickers a lot , a bit less between BeginUpdate/EndUpdate 
     (args.Node as FolderFileNode)?.LoadNodes();     
    }; 

    treeView1.AfterExpand += (sender, args) => 
    {     
     (args.Node as FolderFileNode)?.SetIcon();     
    }; 

    treeView1.AfterCollapse += (sender, args) => 
    { 
     (args.Node as FolderFileNode)?.SetIcon();     
    }; 
}      

class FolderFileNode : TreeNode 
{ 
    private readonly string _path; 

    private readonly bool _isFile; 

    public FolderFileNode(string path) 
    {    
     if(string.IsNullOrWhiteSpace(path)) throw new ArgumentException(nameof(path)); 
     Text = Path.GetFileName(path); 
     _isFile = File.Exists(path); 
     _path = path; 

     if (!_isFile && Directory.EnumerateFileSystemEntries(_path).Any()) 
     { 
      //Will indicate there is children 
      Nodes.Add(new TreeNode()); 
     } 
     SetIcon(); 
    } 

    public void SetIcon() 
    { 
     // image[2] is Folder Open image 
     ImageIndex = _isFile ? ImageIndex = 1 : IsExpanded ? 2 : 0; 
     SelectedImageIndex = _isFile ? ImageIndex = 1 : IsExpanded ? 2 : 0; 
    } 

    private IEnumerable<string> _children; 
    public void LoadNodes() 
    { 
     if (!_isFile && _children == null) 
     { 
      // _children = Directory.EnumerateFileSystemEntries(_path); 
      // Or Add Directories first 
      _children = Directory.EnumerateDirectories(_path).ToList(); 
      ((List<string>) _children).AddRange(Directory.EnumerateFiles(_path)); 

      //Theres one added in the constructor to indicate it has children 
      Nodes.Clear(); 

      Nodes.AddRange(
       _children.Select(x => 
        // co-variant 
        (TreeNode) new FolderFileNode(x)) 
        .ToArray()); 
     } 
    } 
} 
+0

이 위대한 작품을 당신을 감사 게으른 부하로 생각할 수있는 코드의 간단한/최소 금액입니다! 폴더에 +/- 부분 콘텐츠가 있거나 파일과 폴더를 함께 그룹화 함을 나타낼 수있는 방법이 있습니까? 현재는 파일과 폴더를 결합합니다. 파일보다 먼저 폴더를 표시하려면 어떻게해야합니까? 유형별 창 정렬과 유사합니다. – gregyjames

+0

님이 요청한 기능으로 편집했습니다 ... 답변을 수락하지 않으시겠습니까? :) – Dan

+0

죄송합니다. 다시 한 번 감사드립니다! – gregyjames

관련 문제