在这种情况下如何隐藏根节点“temp”文件夹?我想在根节点上设置一个 CssClass 来做到这一点。
TreeView TreeView1 = new TreeView();
protected void Page_Load(object sender, EventArgs e)
{
BuildTree(@"C:\temp");
form1.Controls.Add(TreeView1);
}
private void BuildTree(string root)
{
DirectoryInfo rootDir = new DirectoryInfo(root);
TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
TreeView1.Nodes.Add(rootNode);
TraverseTree(rootDir, rootNode);
}
private void TraverseTree(DirectoryInfo currentDir, TreeNode currentNode)
{
foreach (DirectoryInfo dir in currentDir.GetDirectories())
{
TreeNode node = new TreeNode(dir.Name, dir.FullName);
currentNode.ChildNodes.Add(node);
TraverseTree(dir, node);
}
foreach (FileInfo file in currentDir.GetFiles())
{
TreeNode nodeFile = new TreeNode(file.Name, file.FullName);
currentNode.ChildNodes.Add(nodeFile);
}
}
代码已完成,只需更改桌面的路径即可运行。