【原】根据网站动态目录生成树结构,并用TreeView输出【提供源码下载,有详细注释】...

各位博友好,本着共享的原则,本人花了点时间写了个这棵树,虽然简单,但是应该很实用。

我知道我们可以使用 Web.sitemap 来实现,但是用Web.sitemap 需要预先写好配置,我写这个就不需要考虑配置了。

比如说你的目录改变了,那么自动生成树也会跟着变动,下面贴代码:

ContractedBlock.gif ExpandedBlockStart.gif MyWebsiteTreeView.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyWebsiteTreeView.aspx.cs"
    Inherits
="MyWebsiteTreeView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:TreeView ID="TreeView1" runat="server">
        
</asp:TreeView>
        
<asp:DropDownList ID="ddlRootDirectory" runat="server">
        
</asp:DropDownList>
        
<asp:Button ID="btnGenerateDirectoryTree" runat="server" Text="Ggenerate Directory Tree" 
            onclick
="btnGenerateDirectoryTree_Click" />
    
</div>
    
</form>
</body>
</html>

 

ContractedBlock.gif ExpandedBlockStart.gif MyWebsiteTreeView.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class MyWebsiteTreeView : System.Web.UI.Page
{
    
/// <summary>
    
/// 页面加载时,就动态绑定目录到DropDownList控件
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        
if (!Page.IsPostBack)
        {
            BindDirectoryToDropDownList(ddlRootDirectory);
        }
    }

    
/// <summary>
    
///  调整树节点,使与Firefox 浏览器兼容
    
/// </summary>
    
/// <param name="node"></param>
    void SdjustNodes(TreeNode node)
    {

        
string ab = node.NavigateUrl;
        
//Suodaosoft 是根目录名
        node.NavigateUrl = node.NavigateUrl.Replace("Suodaosoft\\""");
        node.NavigateUrl 
= node.NavigateUrl.Replace("\\""/");//与 Firefox 兼容,IE 能识别“\\”,可是FireFox 不能识别
        if (node.ChildNodes.Count > 0)
        {
            
foreach (TreeNode childNode in node.ChildNodes)
            {
                SdjustNodes(childNode);
            }

        }
    }
    TreeNode OutputDirectory(System.IO.DirectoryInfo directory, TreeNode parentNode)
    {
        
// 检验目录是否为空
        if (directory == nullreturn null;

        
// 为目录建立节点
        TreeNode DirNode = new TreeNode(directory.Name);

        
// 从当前目录得到子目录
        System.IO.DirectoryInfo[] SubDirectories = directory.GetDirectories();

        
// 得出子目录
        for (int DirectoryCount = 0; DirectoryCount < SubDirectories.Length; DirectoryCount++)
        {
            OutputDirectory(SubDirectories[DirectoryCount], DirNode);
        }

        
// 输出当前目录文件
        System.IO.FileInfo[] Files = directory.GetFiles();

        
for (int FileCount = 0; FileCount < Files.Length; FileCount++)
        {
            
//------------------------------------
            
//注意,我注释了if 的条件!
            
//这儿其实有个小技巧
            
//可以筛选你想要的文件
            
//------------------------------------
            
//if (Files[FileCount].Extension == ".htm")
            
//{
            string filename = ConvertFileToRelativePaths(Files[FileCount].FullName, "Suodaosoft");
            DirNode.ChildNodes.Add(
new TreeNode(Files[FileCount].Name, Files[FileCount].Name, "", filename, "_blank"));
            
//}
        }

        
//如果父节点是NULL, 就返回本节点
        
//否则就加到父节点,并返回父节点
        if (parentNode == null)
        {
            
return DirNode;
        }
        
else
        {
            parentNode.ChildNodes.Add(DirNode);
            
return parentNode;
        }
    }
    
/// <summary>
    
/// 转换文件路径
    
/// </summary>
    
/// <param name="fileName">文件名</param>
    
/// <param name="rootName">根目录名</param>
    
/// <returns>目录</returns>
    string ConvertFileToRelativePaths(string fileName, string rootName)
    {
        
return fileName.Substring(fileName.LastIndexOf(rootName));
    }
    
/// <summary>
    
/// 建立目录树
    
/// </summary>
    
/// <param name="tv"></param>
    void GenerateDirectoryTree(TreeView tv)
    {
        tv.Nodes.Clear();

        System.IO.DirectoryInfo RootDir 
= new System.IO.DirectoryInfo(Server.MapPath("~/" + ddlRootDirectory.SelectedValue + "/"));
        
if (ddlRootDirectory.SelectedValue == "Root")
        {
            RootDir 
= new System.IO.DirectoryInfo(Server.MapPath("~/"));
        }

        
// 把目录树输出到一个节点
        TreeNode RootNode = OutputDirectory(RootDir, null);
        
// 把目录树加入根节点
        TreeView1.Nodes.Add(RootNode);
        
foreach (TreeNode node in tv.Nodes)
        {
            SdjustNodes(node);
        }
    }
    
/// <summary>
    
///  把动态目录绑定到DropDownList控件上,供选择目录,如果选择Root,表示项Web工程的根节点
    
/// </summary>
    
/// <param name="ddl"></param>
    void BindDirectoryToDropDownList(DropDownList ddl)
    {
        ddl.Items.Clear();
        System.IO.DirectoryInfo RootDir 
= new System.IO.DirectoryInfo(Server.MapPath("~/"));//根节点
        System.IO.DirectoryInfo[] SubDirectories = RootDir.GetDirectories();
        ddl.Items.Add(
"Root");
        
foreach (DirectoryInfo dir in SubDirectories)
        {
            ddl.Items.Add(dir.Name);
        }
    }
    
/// <summary>
    
/// 根据DropDownList选择的目录,动态产生树
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void btnGenerateDirectoryTree_Click(object sender, EventArgs e)
    {
        GenerateDirectoryTree(TreeView1);
    }
}

 

具体应用,请在下面的这个链接上下载整个应用项目示例:

http://www.cnblogs.com/OceanChen/archive/2009/02/04/1383794.html

 

如果你有什么好的技术观点,你可以参与技术讨论,谢谢你的观点!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值