读取的文件夹和子文件夹在网页里面显示在TreeView中作为菜单,子文件夹下面的文件现在在右边的文件列表里面,点击可以打开或者下载,具体如下图:
我太菜了,实在不知道要什么实现,求老鸟帮忙!

解决方案 »

  1.   

    好好学习TreeView的相关绑定显示的知识
    还有读取文件夹下文件夹及文件的知识,然后结合一下就可以实现了,具体代码不会给你的
      

  2.   


     protected void Page_Load(object sender, EventArgs e)
            {
                    if (Page.IsPostBack == false)
                    {
                            System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/"));                        // output the directory into a node
                            TreeNode RootNode = OutputDirectory(RootDir, null);                        // add the output to the tree
                            TreeView1.Nodes.Add(RootNode);
                    }
            }        TreeNode OutputDirectory(System.IO.DirectoryInfo directory, TreeNode parentNode)
            {
                    // validate param
                    if (directory == null) return null;                // create a node for this directory
                    TreeNode DirNode = new TreeNode(directory.Name);                // get subdirectories of the current directory
                    System.IO.DirectoryInfo[] SubDirectories = directory.GetDirectories();                // output each subdirectory
                    for (int DirectoryCount = 0; DirectoryCount < SubDirectories.Length; DirectoryCount++)
                    {
                            OutputDirectory(SubDirectories[DirectoryCount], DirNode);
                    }                // output the current directories files
                    System.IO.FileInfo[] Files = directory.GetFiles();                for (int FileCount = 0; FileCount < Files.Length; FileCount++)
                    {
                            DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name));
                    }                // if the parent node is null, return this node
                    // otherwise add this node to the parent and return the parent
                    if (parentNode == null)
                    {
                            return DirNode;
                    }
                    else
                    {
                            parentNode.ChildNodes.Add(DirNode);
                            return parentNode;
                    }
            }
      

  3.   

    上面的内容来自
    http://forums.asp.net/p/1021570/1387710.aspx
      

  4.   

    如果我将System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/"));
    改为:System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("D:\\贵州省"));的话会报错,提示“D:\贵州省”不是有效的虚拟路径。
      

  5.   

    要做导航肯定要用Treeview控件,还要得到文件夹和文件信息,查了下帮助,要使用DirectoryInfo类,MSDN的说明是:公开用于创建、移动和枚举目录和子目录的实例方法。无法继承此类。还要使用Request.PhysicalApplicationPath获得当前应用程序根目录的物理路径。解决了关键问题开始编写代码。
    首先添加一个页面命名为LeftTree.aspx,拖进来一个TreeView控件,下面开始后台代码。
    DirectoryInfo类在System.IO命名空间下,引入System.IO命名空间,
    using System.IO;
    在Page_Load事件里首先得到当前应用程序根目录的物理路径。
    string physicalAppPath = Request.PhysicalApplicationPath;
    然后实例化DirectoryInfo以便得到文件夹信息
    DirectoryInfo dirInfo = new DirectoryInfo(physicalAppPath);
    有了DirectoryInfo的实例就可以使用实例的GetFileSystemInfos()方法得到目录中所有文件和子目录,该方法返回FileSystemInfo型数组
    FileSystemInfo[] fileSysInfos = directoryInfo.GetFileSystemInfos();
    下面遍历SileSystemInfo数组将文件或文件夹的名称添加到TreeView控件。
    首先在Page_Load之前定义节点
    TreeNode node;
    开始遍历
    foreach (FileSystemInfo fileSysInfo in fileSysInfos)
    {
    node = new TreeNode(); 
    node.Text = fileSysInfo.Name;
    TreeView1.Nodes.Add(node);
    }
    这样就把根目录下所有文件和文件夹添加到了TreeView控件。
    接下来要实现添加子文件下的文件了,这里需要一个递归。private void GetFileAndDirectoryInfo(DirectoryInfo directoryInfo,TreeNode parentNode)
    {
    FileSystemInfo[] fileSysInfos = directoryInfo.GetFileSystemInfos();
    foreach (FileSystemInfo fileSysInfo in fileSysInfos)
    {
    node = new TreeNode(); 
    node.Text = fileSysInfo.Name;
    parentNode.ChildNodes.Add(node);
    }
    }
    当fileSysInfo的属性为Directory即文件夹时得到该文件夹下所有文件及子文件夹的信息,使用FileSystemInfo的Attributes属性
    在上面的foreach里继续添加如下代码:
    if (fileSysInfo.Attributes == FileAttributes.Directory)
    {
    //文件夹,递归调用GetFileAndDirectoryInfo()
    DirectoryInfo dirinfo = new DirectoryInfo(fileSysInfo.FullName);
    GetFileAndDirectoryInfo(dirinfo, node);
    }
    在Page_Load里调用该方法,此时Page_Load里的代码如下:
    string physicalAppPath = Request.PhysicalApplicationPath;
    DirectoryInfo dirInfo = new DirectoryInfo(physicalAppPath);
    node = new TreeNode();
    node.Text = dirInfo.Name;
    TreeView1.Nodes.Add(node);
    GetFileAndDirectoryInfo(dirInfo, node);这样就可以以树状形式展示所有文件及文件夹了。但是我不想要cs文件等其他不方便在浏览器上显示的文件。接下来判断文件扩展名,不显示不想看到的文件。开始想使用枚举后来想用数组来定义要显示文件的扩展名信息,但是好像都不好判断一个字符串是否存在于一个枚举变量或字符串数组,干脆直接用字符串啦。
    在Page_Load外定义变量:
    string showExtensions = ".asp.aspx.htm.html.txt.xml.";    //要显示的文件的扩展名
    接下来在foreach的if语句后面添加else分支语句
    else 
    {
    //文件
    if (showExtensions.IndexOf(fileSysInfo.Extension) > -1)
    {   
    node = new TreeNode(); 
    node.Text = fileSysInfo.Name;
    parentNode.ChildNodes.Add(node);
    }
    }
    这样if语句上面的三条添加节点的语句就需要拿到if里面来了。
    这样就只得到了我想要的文件类型。
    但是TreeView只有一个根节点,这样很难看,TreeView允许添加多个根节点,比如我们在显示产品类别的时候就会用到多个根节点。接下来就去掉这个根节点。
    以前的做法是创建这个数的拷贝然后去掉根节……,反正感觉当时做的很麻烦,今天用如下方法实现。
    给GetFileAndDirectoryInfo()方法添加一个参数,用来标示第二个参数也就是parentNode是否是根节点
    private void GetFileAndDirectoryInfo(DirectoryInfo directoryInfo, TreeNode parentNode,bool isRoot)
    用如下代码替换掉foreach里parentNode.ChildNodes.Add(node);
    if (isRoot) 
    {
    TreeView1.Nodes.Add(node); 

    else 

    parentNode.ChildNodes.Add(node); 
    }
    这样去掉了难看的根节点。到此我们还没有给节点设置NavigateUrl属性,TreeView的节点在没有设置NavigateUrl属性或NavigateUrl值为空时默认产生一个SelectedNodeChange事件,我们为文件节点设置NavigateUrl属性,使得点击时打开我们的文件,设置文件夹节点的TreeNodeSelectAction属性为Expand,使得点击节点时展开或关闭节点而不产生SelectedNodeChange事件,在上面的if-else语句后添加如下代码:
    if (fileSysInfo.Attributes == FileAttributes.Directory) 

    node.SelectAction = TreeNodeSelectAction.Expand; 

    else 

    node.NavigateUrl = node.ValuePath; //节点只有在添加之后才能得到ValuPath属性和Depth属性
    }
    下面添加一个框架页。2005却不支持框架,也不提供添加框架页,要是这样树节点还要个Target属性干什么啊。先添加一个Html页命名为Default.htm,用2003做一个框架页将代码拷贝过来,代码如下:
    <frameset rows="*" border="0" frameSpacing="0" frameBorder="0"> 
    <frameset cols="220,*"> 
    <frame name="LeftTree" src="LeftTree.aspx"> 
    <frame name="Main" src="Main.aspx"> 
    </frameset> 
    <noframes> 
    </noframes> 
    </frameset>
    在添加一个Main.aspx文件。然后在LeftTree.aspx.cs文件的node.NavigateUrl = node.ValuePath;代码后添加语句
    node.Target = "Main";
    这样就OK了。
    完整代码附后。我就做了这里了,接下来的问题是:当文件夹下没有可显示的文件时文件夹还会被添加到树中,很显然那是多余的,怎么样去掉呢?好像还得来一次递归。如果你有好办法可以一起来探讨。第一次写这东东好像写的很罗嗦,请多批评指正,共同学习。完整代码:
    LeftTree.aspx:
    <head runat="server">
        <title>无标题页</title>
        <style type="text/css">
        body{ background-color:#d9eef5 }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TreeView ID="TreeView1" runat="server" Font-Size="12px" ForeColor="Black">
            </asp:TreeView>
        </div>
        </form>
    </body>LeftTree.aspx.cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;public partial class _LeftTree : System.Web.UI.Page
    {
        TreeNode node;    string showExtensions = ".asp.aspx.htm.html.webconfig.txt.xml.";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //string mapPath = Server.MapPath("~");   //获得当前应用程序根目录的物理路径
                string physicalAppPath = Request.PhysicalApplicationPath;   //</div>
                DirectoryInfo dirInfo = new DirectoryInfo(physicalAppPath);
                node = new TreeNode();
                node.Text = dirInfo.Name;
                GetFileAndDirectoryInfo(dirInfo, node, true);
                TreeView1.ShowLines = true;
                TreeView1.Font.Size = FontUnit.Parse("12px");
                TreeView1.CollapseAll();
            }
        }    void GetFileAndDirectoryInfo(DirectoryInfo directoryInfo, TreeNode parentNode,bool isRoot)
        {
            FileSystemInfo[] fileSysInfos = directoryInfo.GetFileSystemInfos();
            foreach (FileSystemInfo fileSysInfo in fileSysInfos)
            {
                if (fileSysInfo.Attributes == FileAttributes.Directory)
                {
                    //文件夹
                    AddNode(parentNode, isRoot, fileSysInfo);                DirectoryInfo dirinfo = new DirectoryInfo(fileSysInfo.FullName);
                    GetFileAndDirectoryInfo(dirinfo, node, false);
                }
                else
                {
                    //文件
                    if (showExtensions.IndexOf(fileSysInfo.Extension) > -1)
                    {
                        AddNode(parentNode, isRoot, fileSysInfo);
                    }
                }
            }
        }    private void AddNode(TreeNode parentNode, bool isRoot, FileSystemInfo fileSysInfo)
        {
            node = new TreeNode();
            node.Text = fileSysInfo.Name;        if (isRoot)
            {
                TreeView1.Nodes.Add(node);
            }
            else
            {
                parentNode.ChildNodes.Add(node);
            }        if (fileSysInfo.Attributes == FileAttributes.Directory)
            {
                node.SelectAction = TreeNodeSelectAction.Expand;
            }
            else
            {
                node.NavigateUrl = node.ValuePath; //节点只有在添加之后才能得到ValuPath属性和Depth属性
                node.Target = "Main";
            }    }
    }Default.html
    <frameset rows="*" border="0" frameSpacing="0" frameBorder="0">
       <frameset cols="220,*">
        <frame name="LeftTree" src="LeftTree.aspx">
        <frame name="Main" src="Main.aspx">
       </frameset>
       <noframes>
       </noframes>
    </frameset>使用树控件显示文件夹下的所有的文件来自:使用树控件显示文件夹下的所有的文件:http://blog.163.com/aspnetshop@126/blog/static/1731313352009719112448548/