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)
var rootDirectoryInfo = new DirectoryInfo(path);
private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
TreeNode tn = CreateDirectoryNode(directory);
catch (UnauthorizedAccessException x)
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.
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 :
C:\ // <- C:\ is the selected/expanded node |---Documents and Settings |---Empty Folder |---Program Files |---Program Files (x86) |---Windows
"*"without expanding that parent node. (as you know
"*"is not allowed for actual directory names, but okay for TreeView)
C:\ [+]-Documents and Settings |--Empty Folder [-]-Program Files | |--[*] <- dummy Child Node (expanded here but collapsed in TreeView) [+]-Program Files (x86) [+]-Windows
BeforeExpandevent 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)