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

  • 相关阅读:
    并不对劲的loj3124:uoj477:p5405:[CTS2019]氪金手游
    并不对劲的loj6498. 「雅礼集训 2018 Day2」农民
    并不对劲的loj2251:p3688[ZJOI2017]树状数组
    并不对劲的loj2050:p3248:[HNOI2016]树
    并不对劲的BJOI2020
    并不对劲的loj3110:p5358:[SDOI2019]快速查询
    并不对劲的loj3111:p5359:[SDOI2019]染色
    (困难) CF 484E Sign on Fence,整体二分+线段树
    算法录 之 拓扑排序和欧拉路径。
    数据结构录 之 BST的高级应用。
  • 原文地址:https://www.cnblogs.com/cosiray/p/1552705.html
Copyright © 2011-2022 走看看