c# - What algorithm can I use to recursively load an entire directory, starting from a specified path? -
i'm building custom file dialog, , problem taking long load.
the dialog begins initialdirectory
property, , looking find way load directory tree initialdirectory
first, followed rest of directories in background thread.
for example, if initialdirectory
c:\users\user12345\mydocuments
, should load folders in
c:\ c:\users c:\user12345 c:\users\user12345\mydocuments
then kick off background thread load remaining directories.
is there fast , easy way use recursion load first initialdirectory
, else, without duplicating items?
i'm struggling find high-performing way this, since checking existance of folder code if (!directory.contains(f => f.fullname == folder.fullname))
slows down load quite bit.
my current code load full directory looks this:
private void loaddirectory() { string root = @"c:\"; var rootnode = new directorymodel() { name = root, fullname = root }; this.directory.add(rootnode); directoryinfo info = new directoryinfo(root); ienumerable<directoryinfo> subdirectories = info.getdirectories() .where(d => ((d.attributes & fileattributes.hidden) != fileattributes.hidden) && ((d.attributes & fileattributes.system) != fileattributes.system)); loaddirectories(subdirectories, root); } private void loaddirectories(ienumerable<directoryinfo> subdirs, string parentname) { ienumerable<directoryinfo> subdirectories; foreach (directoryinfo folder in subdirs) { var node = new directorymodel() { name = folder.name, fullname = folder.fullname, parentname = parentname }; directory.add(node); try { subdirectories = folder.getdirectories("*", searchoption.topdirectoryonly) .where(d => ((d.attributes & fileattributes.hidden) != fileattributes.hidden) && ((d.attributes & fileattributes.system) != fileattributes.system)); } catch (unauthorizedaccessexception e) { continue; } catch (system.io.directorynotfoundexception e) { continue; } if (subdirectories.count() != 0) loaddirectories(subdirectories, folder.fullname); } }
note directory
collection flat collection - there no hierarchy in data models.
it depends on how present user data, imo.
if use treeview
control, can avoid load data in background, load root of tree (like as understood).
in way, having procedure loads sub-directory names , files of specified directory need wait until user clicks on [+]
in order see directory content interesting to him. run procedure , populate tree.
this highest performant way known me deal kind of stuff, , find pattern in "explorer like" products. performance here achieved not refined algorithm, defining more convenient behavioral model.
hope helps.
Comments
Post a Comment