当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Confusion about directory tree

问题描述:

I am trying to list all the files in my computer .And this code below meets my expectations.I have two drives in my computer C and D .If the user select D drive everyting is ok.But When C drive selected(Not one of the sub folders)application not responding.

 private void ListDirectory(TreeView treeView, string path)

{

treeView.Nodes.Clear();

var rootDirectoryInfo = new DirectoryInfo(path);

treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));

}

private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)

{

var directoryNode = new TreeNode(directoryInfo.Name);

try

{

foreach (var directory in directoryInfo.GetDirectories())

{

//directoryNode.Nodes.Add(CreateDirectoryNode(directory));

TreeNode tn = CreateDirectoryNode(directory);

directoryNode.Nodes.Add(tn);

}

}

catch (UnauthorizedAccessException x)

{

}

return directoryNode;

}

网友答案:

A logical reason why this is happening is the massive amount of folders on your system drive. Instead of loading all items at once, you should load the items 'on-demand'.

You should load all master folders in the tree, and then hook op the TreeView.BeforeSelect event. Inside that event, you could load the sub items of the tree, possibly in a Task to make it asynchronous.

An easy way to store additional information on the item you are working on, the the Tag property of the TreeNode. You can set it to a custom object you make to store if you already have loaded the sub items for example.

网友答案:

Possibly because C: is the OS drive and it will have lots of folders. You are recursively calling this method for each sub directly in C: drive, which by my experience will have thousands of directories.

One possible solution is to display on immediate child directories of the tree node and then only load the sub-directories when the node is expanded (On-demand loading). Or show a progress bar and use a BackgroundWorker to get the sub-directories so that UI is still responsive.

网友答案:

Complement answer for further readers :

  1. Load the directories of a given path in your treeview (the subfolder of a selected node) Note : Load the appropriate instance(s) of FileSystem Watcher.

.

C:\  // <- C:\ is the selected/expanded node
 |---Documents and Settings
 |---Empty Folder
 |---Program Files
 |---Program Files (x86)
 |---Windows

.

  1. For each of those subfolders, check if it contains subfolder. If it's the case, add a single child node called "*" without expanding that parent node. (as you know "*" is not allowed for actual directory names, but okay for TreeView)

Clickable [+] appears.

.

C:\
[+]-Documents and Settings
 |--Empty Folder
[-]-Program Files
 |   |--[*]  <- dummy Child Node (expanded here but collapsed in TreeView)
[+]-Program Files (x86)
[+]-Windows

.

  1. Capture the BeforeExpand event of the node, and if the first child node is called "*", delete that child node, and loads the subfolders of that node (while adding "*" for each subfolder that contains further subfolders)

.

C:\
[+]-Documents and Settings
 |--Empty Folder
[-]-Program Files
 |  [+]-ATI
 |  [+]-Classic Shell
 |  [+]-Common Files
 |   |--Empty Folder
 |  [+]-Microsoft
 |  [+]-TOSHIBA
 |  [+]-Windows NT
[+]-Program Files (x86)
[+]-Windows

.

BeforeExpand ensures you load every subfolder before showing them.

Note : Your application may fail to gain access to some folders. Be sure to catch such exceptions.

Use another thread (or a timer) to unload the child nodes that has been collapsed for a substantial amount of time to free memory.

You may use a BackgroundWorker to preload the subfolders of visible folders in your TreeView (but not their subfolders) This move is not recommended as it can lead to unexpected behaviours if you're interfering with the Work in progress (do this only if you've properly handled such cases)

分享给朋友:
您可能感兴趣的文章:
随机阅读: