我有一个用于显示目录结构的树视图。我试图通过在节点扩展上加载子节点来减少加载时间。有没有办法做到这一点?
下面是我目前用来填充树视图的代码:
protected void Page_Load(object sender, EventArgs e) {
BuildTree(Request.QueryString["path"]);
}
private void BuildTree(string dirPath)
{
//get root directory
System.IO.DirectoryInfo rootDir = new System.IO.DirectoryInfo(dirPath);
//create and add the root node to the tree view
TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
TreeView1.Nodes.Add(rootNode);
//begin recursively traversing the directory structure
TraverseTree(rootDir, rootNode);
}
private void TraverseTree(System.IO.DirectoryInfo currentDir, TreeNode currentNode)
{
//loop through each sub-directory in the current one
foreach (System.IO.DirectoryInfo dir in currentDir.GetDirectories())
{
//create node and add to the tree view
TreeNode node = new TreeNode(dir.Name, dir.FullName);
currentNode.ChildNodes.Add(node);
//recursively call same method to go down the next level of the tree
TraverseTree(dir, node);
}
foreach (System.IO.FileInfo file in currentDir.GetFiles())
{
TreeNode node = new TreeNode(file.Name, file.FullName);
currentNode.ChildNodes.Add(node);
}
}