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() + "/", "  ┣");
                    }
                }
            }
        }

  • 相关阅读:
    POJ 2251 Dungeon Master
    HDU 3085 Nightmare Ⅱ
    CodeForces 1060 B Maximum Sum of Digits
    HDU 1166 敌兵布阵(树状数组)
    HDOJ 2050 折线分割平面
    HDU 5879 Cure
    HDU 1878 欧拉回路
    HDU 6225 Little Boxes
    ZOJ 2971 Give Me the Number
    HDU 2680 Choose the best route
  • 原文地址:https://www.cnblogs.com/cosiray/p/1552705.html
Copyright © 2011-2022 走看看