zoukankan      html  css  js  c++  java
  • 【原创】C# 将虚拟目录下文件转换成DataTable

    //将虚拟目录转换成DataTable
        public static DataTable DirToDataTable(string strDir)
        {
            try
            {
                DataTable FileTable = new DataTable();

                FileTable.Columns.Add(new DataColumn("name", typeof(String)));
                FileTable.Columns.Add(new DataColumn("size", typeof(Int32)));
                FileTable.Columns.Add(new DataColumn("type", typeof(String)));
                FileTable.Columns.Add(new DataColumn("date", typeof(DateTime)));
                FileTable.Columns.Add(new DataColumn("fullname", typeof(String)));
                FileTable.Columns.Add(new DataColumn("url", typeof(String)));

                if (!Directory.Exists(strDir))
                {
                    DirectoryInfo Dir = new DirectoryInfo(HttpContext.Current.Server.MapPath(strDir));

                    foreach (FileInfo f in Dir.GetFiles())
                    {
                        DataRow FileRow = FileTable.NewRow();
                        FileRow[0] = f.Name;
                        FileRow[1] = f.Length / 1024;
                        FileRow[2] = f.Extension ?? "unknow";
                        FileRow[3] = f.LastWriteTime;
                        FileRow[4] = f.FullName;
                        FileRow[5] = strDir + f.Name;
                        FileTable.Rows.Add(FileRow);
                    }
                }

                return FileTable;
            }
            catch
            {
                return null;
            }
        }

    //包含子目录及文件
    public static DataTable GetAllDirFileToDataTable(string dirPath) //参数dirPath为指定的目录
        {
            try
            {
                if (!Directory.Exists(dirPath))
                {
                    DataTable FileTable = new DataTable();

                    FileTable.Columns.Add(new DataColumn("name", typeof(String)));
                    FileTable.Columns.Add(new DataColumn("size", typeof(Int32)));
                    FileTable.Columns.Add(new DataColumn("type", typeof(String)));
                    FileTable.Columns.Add(new DataColumn("date", typeof(DateTime)));
                    FileTable.Columns.Add(new DataColumn("fullname", typeof(String)));
                    FileTable.Columns.Add(new DataColumn("url", typeof(String)));

                    DirectoryInfo Dir = new DirectoryInfo(HttpContext.Current.Server.MapPath(dirPath));

                    //获得当前文件夹下所有文件
                    foreach (FileInfo f in Dir.GetFiles())
                    {
                        DataRow FileRow = FileTable.NewRow();
                        FileRow[0] = f.Name;
                        FileRow[1] = f.Length / 1024;
                        FileRow[2] = f.Extension ?? "unknow";
                        FileRow[3] = f.LastWriteTime;
                        FileRow[4] = f.FullName;
                        FileRow[5] = dirPath + f.Name;
                        FileTable.Rows.Add(FileRow);
                    }

                    //获得子目录下所有文件
                    foreach (DirectoryInfo d in Dir.GetDirectories())
                    {
                        DataTable dt = GetAllDirFileToDataTable(dirPath + d.Name.ToString() + "/");
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                DataRow FileRow = FileTable.NewRow();
                                foreach (DataColumn dc in dt.Columns)
                                {
                                    FileRow[dc.ColumnName] = dr[dc.ColumnName];
                                }
                                FileTable.Rows.Add(FileRow);
                            }
                        }
                    }

                    return FileTable;
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        //绑定指定虚拟目录下拉框(包括子目录)
        public static void BindDir(DropDownList dd, string dir, string icon)
        {
            if (!Directory.Exists(dir))
            {
                DirectoryInfo Dir = new DirectoryInfo(HttpContext.Current.Server.MapPath(dir));
                if (Dir.GetDirectories().Length > 0)
                {
                    foreach (DirectoryInfo d in Dir.GetDirectories())
                    {
                        dd.Items.Add(new ListItem(icon + d.Name.ToString(), HttpContext.Current.Server.UrlEncode(dir + d.Name.ToString() + "/")));
                        BindDir(dd, dir + d.Name.ToString() + "/", "  ┣");
                    }
                }
            }
        }

  • 相关阅读:
    Largest Rectangle in Histogram, 求矩形图中最大的长方形面积
    MergeSortedArray,合并两个有序的数组
    Remove Duplicates from Sorted List ,除去链表中相邻的重复元素
    Word Search, 在矩阵中寻找字符串,回溯算法
    SubSets,SubSets2, 求数组所有子集
    Longest Substring Without Repeating Characters,求没有重复字符的最长字串
    Minimum Window Substring, 包含子串的最小窗口,双指针
    Sort Colors,颜色排序
    Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。
    EditDistance,求两个字符串最小编辑距离,动态规划
  • 原文地址:https://www.cnblogs.com/cosiray/p/1552705.html
Copyright © 2011-2022 走看看