欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 获取指定文件夹下所有子目录及文件(树形)

程序员文章站 2023-09-20 11:11:47
#region 获取指定文件夹下所有子目录及文件(树形)         /***...
#region 获取指定文件夹下所有子目录及文件(树形)
        /****************************************
         * 函数名称:getfoldall(string path)
         * 功能说明:获取指定文件夹下所有子目录及文件(树形)
         * 参    数:path:详细路径
         * 调用示列:
         *           string strdirlist = server.mappath("templates");       
         *           this.literal1.text = ec.fileobj.getfoldall(strdirlist);  
        *****************************************/
        /// <summary>
        /// 获取指定文件夹下所有子目录及文件
        /// </summary>
        /// <param name="path">详细路径</param>
        public static string getfoldall(string path)
        {

            string str = "";
            directoryinfo thisone = new directoryinfo(path);
            str = listtreeshow(thisone, 0, str);
            return str;

        }

        /// <summary>
        /// 获取指定文件夹下所有子目录及文件函数
        /// </summary>
        /// <param name="thedir">指定目录</param>
        /// <param name="nlevel">默认起始值,调用时,一般为0</param>
        /// <param name="rn">用于迭加的传入值,一般为空</param>
        /// <returns></returns>
        public static string listtreeshow(directoryinfo thedir, int nlevel, string rn)//递归目录 文件
        {
            directoryinfo[] subdirectories = thedir.getdirectories();//获得目录
            foreach (directoryinfo dirinfo in subdirectories)
            {

                if (nlevel == 0)
                {
                    rn += "├";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i <= nlevel; i++)
                    {
                        _s += "│ ";
                    }
                    rn += _s + "├";
                }
                rn += "<b>" + dirinfo.name.tostring() + "</b><br />";
                fileinfo[] fileinfo = dirinfo.getfiles();   //目录下的文件
                foreach (fileinfo finfo in fileinfo)
                {
                    if (nlevel == 0)
                    {
                        rn += "│ ├";
                    }
                    else
                    {
                        string _f = "";
                        for (int i = 1; i <= nlevel; i++)
                        {
                            _f += "│ ";
                        }
                        rn += _f + "│ ├";
                    }
                    rn += finfo.name.tostring() + " <br />";
                }
                rn = listtreeshow(dirinfo, nlevel + 1, rn);


            }
            return rn;
        }

 

        /****************************************
         * 函数名称:getfoldall(string path)
         * 功能说明:获取指定文件夹下所有子目录及文件(下拉框形)
         * 参    数:path:详细路径
         * 调用示列:
         *            string strdirlist = server.mappath("templates");      
         *            this.literal2.text = ec.fileobj.getfoldall(strdirlist,"tpl","");
        *****************************************/
        /// <summary>
        /// 获取指定文件夹下所有子目录及文件(下拉框形)
        /// </summary>
        /// <param name="path">详细路径</param>
        ///<param name="dropname">下拉列表名称</param>
        ///<param name="tplpath">默认选择模板名称</param>
        public static string getfoldall(string path,string dropname,string tplpath)
        {
            string strdrop = "<select name=\"" + dropname + "\" id=\"" + dropname + "\"><option value=\"\">--请选择详细模板--</option>";
            string str = "";
            directoryinfo thisone = new directoryinfo(path);
            str = listtreeshow(thisone, 0, str,tplpath);
            return strdrop+str+"</select>";

        }

        /// <summary>
        /// 获取指定文件夹下所有子目录及文件函数
        /// </summary>
        /// <param name="thedir">指定目录</param>
        /// <param name="nlevel">默认起始值,调用时,一般为0</param>
        /// <param name="rn">用于迭加的传入值,一般为空</param>
        /// <param name="tplpath">默认选择模板名称</param>
        /// <returns></returns>
        public static string listtreeshow(directoryinfo thedir, int nlevel, string rn,string tplpath)//递归目录 文件
        {
            directoryinfo[] subdirectories = thedir.getdirectories();//获得目录

            foreach (directoryinfo dirinfo in subdirectories)
            {

                rn += "<option value=\"" + dirinfo.name.tostring() + "\"";
                if (tplpath.tolower() == dirinfo.name.tostring().tolower())
                {
                    rn += " selected ";
                }
                rn += ">";

                if (nlevel == 0)
                {
                    rn += "┣";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i <= nlevel; i++)
                    {
                        _s += "│ ";
                    }
                    rn += _s + "┣";
                }
                rn += "" + dirinfo.name.tostring() + "</option>";


                fileinfo[] fileinfo = dirinfo.getfiles();   //目录下的文件
                foreach (fileinfo finfo in fileinfo)
                {
                    rn += "<option value=\"" + dirinfo.name.tostring()+"/"+finfo.name.tostring() + "\"";
                    if (tplpath.tolower() == finfo.name.tostring().tolower())
                    {
                        rn += " selected ";
                    }
                    rn += ">";

                    if (nlevel == 0)
                    {
                        rn += "│ ├";
                    }
                    else
                    {
                        string _f = "";
                        for (int i = 1; i <= nlevel; i++)
                        {
                            _f += "│ ";
                        }
                        rn += _f + "│ ├";
                    }
                    rn += finfo.name.tostring() + "</option>";
                }
                rn = listtreeshow(dirinfo, nlevel + 1, rn, tplpath);


            }
            return rn;
        }
        #endregion