Ok,
I am creating a directory browsing app and am using the obout.com
TreeView control. This is exactly what I was looking for, as of right
now my code is quite sloppy and I plan on cleaning it up.
Here is my setup:
Client--->IIS Server--->File Server
when I populate the treeview, I do so all on the first time they hit
the page. this is very costly for the IIS Server CPU and it takes
awhile to load.
There is an option in this treeview to load the data from a specially
formatted XML file. Would reading all the dirs, subdirs and files into
an XML file first, then loading this XML file into Treeview help at
all? I guess I should start by cleaning up my code... here it is below:
m_UserPath is the logged in user's directory that I am getting all the
dirs and files for.
It enters PopulateTree first.
any help would be appreciated!! thanks!
public void PopulateTree() {
// Add the root name with username and My Computer icon
oTree.AddRootNode(m_UserName, "xpMyComp.gif");
// Root Directory
// This part gets all the dirs for the root, then enters the
GetContents
// each time it finds a sub directory, it will drill down all the
way
// until it is complete
int i = 0;
foreach (string dir in
System.IO.Directory.GetDirectories(m_UserPath)) {
string dirName = dir.Substring(dir.LastIndexOf("\\") + 1);
oTree.Add("root", "a" + i, dirName, false, null, null);
oTree.
GetContents("a" + i, m_UserPath + "\\" + dirName);
// Get files under the root directories
string[] files = System.IO.Directory.GetFiles(m_UserPath + "\\" +
dirName);
if (files.Length > 0) {
int x = 0;
foreach (string file in files) {
string fileName = file.Substring(file.LastIndexOf("\\") + 1);
oTree.Add("a" + i, "a" + i + "_" + x, fileName, false,
"help_page.gif", null);
x += 1;
}
}
i += 1;
}
// Get Files under the root dir
foreach (string file in System.IO.Directory.GetFiles(m_UserPath)) {
string fileName = file.Substring(file.LastIndexOf("\\") + 1);
oTree.Add("root", "a" + i, fileName, false, "help_page.gif", null);
i += 1;
}
oTree.FolderIcons = "TreeIcons/Icons";
oTree.FolderStyle = "TreeIcons/Styles/Classic";
// Write treeview to your page.
TreeView.Text = oTree.HTML();
}
// This will traverse the p_Folder passed to it, each time
// it finds a subDir, it will re-enter the method and add
// subfolders recursively, then it will add files for each dir
public void GetContents(string p_ParentID, string p_Folder) {
// Dirs
string[] subDir = System.IO.Directory.GetDirectories(p_Folder);
if (subDir.Length > 0) {
int x = 0;
foreach (string sub in subDir) {
string subDirName = sub.Substring(sub.LastIndexOf("\\") + 1);
string thisID = p_ParentID + "_" + x;
oTree.Add(p_ParentID, thisID, subDirName, false, null, null);
// Now let's find if there is anymore subdirs
string[] subDir2 = System.IO.Directory.GetDirectories(sub);
if (subDir2.Length > 0) {
GetContents(thisID,sub);
}
// Check for files under this directory, and add them if relevant
string[] files = System.IO.Directory.GetFiles(p_Folder + "\\" +
subDirName);
if (files.Length > 0) {
foreach (string file in files) {
x += 1;
string folderID = thisID;
string fileID = thisID + "_" + x;
string fileName = file.Substring(file.LastIndexOf("\\") + 1);
oTree.Add(folderID, fileID, fileName, false, "help_page.gif",
null);
}
}
x += 1;
}
}
}