C# DropDownList绑定文件夹

首先创建一个类,类名称为FileControl,

         /// <summary>
        /// 获取制定文件夹下面的文件夹
        /// </summary>
        /// <param name="FileDir">文件夹名称</param>
        /// <returns></returns>
        public static DataTable FileDirList(string FileDir)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("dir_id");
            dt.Columns.Add("dir_name");
            dt.Columns.Add("dir_dir");
            dt.Columns.Add("dir_create_time");

            try
            {
                string strXmlZipDirectory = FileDir;
                if (Directory.Exists(strXmlZipDirectory))
                {
                    DirectoryInfo di = new DirectoryInfo(strXmlZipDirectory);
                    DirectoryInfo[] ds = di.GetDirectories();
                    if (ds.Length > 0)
                    {
                        int i = 0;
                        foreach (DirectoryInfo tmpDirs in ds)
                        {
                            DataRow dr = dt.NewRow();
                            dr[0] = i.ToString();
                            dr[1] = tmpDirs.Name;
                            dr[2] = tmpDirs.FullName;
                            dr[3] = tmpDirs.CreationTime;
                            dt.Rows.InsertAt(dr, i);
                            i++;
                        }
                        return dt;
                    }
                    else
                    {
                        return dt;
                    }
                }
                else
                {
                    return dt;
                }
            }
            catch  
            {
               
                return dt;
            }
        }
        /// <summary>
        /// 获取指定文件夹下的文件,附加后缀过滤
        /// </summary>
        /// <param name="FileDir">文件夹名称</param>
        /// <param name="FileExtension">文件后缀</param>
        /// <returns></returns>
        public static DataTable FileList(string FileDir, string FileExtension)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("file_id");
            dt.Columns.Add("file_name");
            dt.Columns.Add("file_dir");
            dt.Columns.Add("file_url");
            dt.Columns.Add("file_size");
            qdt.Columns.Add("file_create_time");
            dt.Columns.Add("file_Extension");
            dt.Columns.Add("file_IsReadOnly");

            try
            {
                string strXmlZipDirectory = FileDir;
                if (Directory.Exists(strXmlZipDirectory))
                {
                    DirectoryInfo di = new DirectoryInfo(strXmlZipDirectory);
                    FileInfo[] FI = di.GetFiles(FileExtension);
                    if (FI.Length > 0)
                    {
                        int i = 0;
                        foreach (FileInfo tmpFI in FI)
                        {
                            DataRow dr = dt.NewRow();
                            dr[0] = i.ToString();
                            dr[1] = tmpFI.Name;
                            dr[2] = tmpFI.Directory;
                            dr[3] = tmpFI.FullName;
                            dr[4] = tmpFI.Length;
                            dr[5] = tmpFI.CreationTime;
                            dr[6] = tmpFI.Extension;
                            dr[7] = tmpFI.IsReadOnly;
                            dt.Rows.InsertAt(dr, i);
                            i++;
                        }
                        return dt;
                    }
                    else
                    {
                        return dt;
                    }
                }
                else
                {
                    return dt;
                }
            }
            catch  
            {
                
                return dt;
            }
        }
        /// <summary>
        /// 获取指定文件夹下面的文件名称
        /// </summary>
        /// <param name="FileDir">文件夹名称</param>
        /// <returns></returns>
        public static DataTable FileList(string FileDir)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("file_id");
            dt.Columns.Add("file_name");
            dt.Columns.Add("file_dir");
            dt.Columns.Add("file_url");
            dt.Columns.Add("file_size");
            dt.Columns.Add("file_create_time");
            dt.Columns.Add("file_Extension");
            dt.Columns.Add("file_IsReadOnly");

            try
            {
                string strXmlZipDirectory = FileDir;
                if (Directory.Exists(strXmlZipDirectory))
                {
                    DirectoryInfo di = new DirectoryInfo(strXmlZipDirectory);
                    FileInfo[] FI = di.GetFiles("*.*");
                    if (FI.Length > 0)
                    {
                        int i = 0;
                        foreach (FileInfo tmpFI in FI)
                        {
                            DataRow dr = dt.NewRow();
                            dr[0] = i.ToString();
                            dr[1] = tmpFI.Name;
                            dr[2] = tmpFI.Directory;
                            dr[3] = tmpFI.FullName;
                            dr[4] = tmpFI.Length;
                            dr[5] = tmpFI.CreationTime;
                            dr[6] = tmpFI.Extension;
                            dr[7] = tmpFI.IsReadOnly;
                            dt.Rows.InsertAt(dr, i);
                            i++;
                        }
                        return dt;
                    }
                    else
                    {
                        return dt;
                    }
                }
                else
                {
                    return dt;
                }
            }
            catch  
            {
               
                return dt;
            }
        }

在C#代码的调用和方法为

  /// <summary>
        /// 绑定后台文件夹
        /// </summary>
        protected void BackPageDir()
        {
            ListItem item2 = new ListItem("根目录", ""); 
            this.DropDownList_BackPageDir.Items.Add(item2);
            foreach (DataRow rows in FileControl.FileDirList(Server.MapPath("../")).Rows)
            {
                ListItem items = new ListItem(rows["dir_name"].ToString(), rows["dir_name"].ToString());
                this.DropDownList_BackPageDir.Items.Add(items);
            }
        }

 /// <summary>
        /// 绑定文件夹内的*.aspx文件或者*.html文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DropDownList_BackPageDir_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strPath = "../";
            if (DropDownList_BackPageDir.SelectedValue.Length > 0)
            {
                strPath = "../" + DropDownList_BackPageDir.SelectedValue + "/";//获得选中的文件名
            }
            this.DropDownList_BackPage.Items.Clear();//清除Backpage内的数据
            foreach (DataRow rows in FileControl.FileList(Server.MapPath(strPath), "*.aspx").Rows)
            {
                //获取后缀为.aspx的文件
                ListItem items = new ListItem(rows["file_name"].ToString(), rows["file_name"].ToString());
                DropDownList_BackPage.Items.Add(items);
            }

            foreach (DataRow rows in FileControl.FileList(Server.MapPath(strPath), "*.html").Rows)
            {
                //获取后缀为.html的文件
                ListItem items = new ListItem(rows["file_name"].ToString(), rows["file_name"].ToString());
                DropDownList_BackPage.Items.Add(items);
            }
        }

 

posted @ 2013-11-05 15:34  贝小贝  阅读(389)  评论(0编辑  收藏  举报