asp.net 遍历文件夹下全部子文件夹并绑定到gridview上

时间:2023-03-10 00:25:30
asp.net 遍历文件夹下全部子文件夹并绑定到gridview上

遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上

    1. Helper app_Helper = new Helper();
    2. DataSet ds = new DataSet();
    3. DataTable dt = new DataTable();
    4. protected void Page_Load(object sender, EventArgs e)
    5. {
    6. gvwBind();
    7. }
    8. #region 绑定GridView
    9. /// <summary>
    10. /// 绑定GridView
    11. /// </summary>
    12. protected void gvwBind()
    13. {
    14. INI.INIPath iniINIPath = new INI.INIPath();
    15. INI.INIFile iniINIFile = new INI.INIFile(iniINIPath.getINIPath());
    16. string FolderNames = app_Helper.GetAllFolder(Server.MapPath("../../APPDIR"));//获得APPDIR文件夹下所有子文件夹名
    17. string[] FolderName = new string[FolderNames.Split(',').Length - 1];//声明个数组为了保存文件夹绝对路径
    18. FolderName = FolderNames.Split(',');//给文件夹绝对路径数组赋值
    19. dt.Columns.Add("LKNAME", typeof(string));
    20. dt.Columns.Add("qdjsjm", typeof(string));
    21. dt.Columns.Add("qdipdz", typeof(string));
    22. for (int i = 0; i < FolderName.Length - 1; i++)
    23. {
    24. DataRow row = dt.NewRow();
    25. FolderName[i] = FolderName[i].Substring(FolderName[i].LastIndexOf('\\') + 1);//截取文件夹名
    26. row["LKNAME"] = FolderName[i];//给 LKNAME 字符赋值
    27. if (!iniINIFile.IniReadValue("dzjclkzp", FolderName[i]).Equals(""))//配置文件中是否存在相同文件夹名的key
    28. {
    29. row["qdjsjm"] = iniINIFile.IniReadValue("dzjclkzp", FolderName[i]).Split(',')[0];//给qdjsjm字段赋值
    30. row["qdipdz"] = iniINIFile.IniReadValue("dzjclkzp", FolderName[i]).Split(',')[1];//给qdipdz字段赋值
    31. }
    32. dt.Rows.Add(row);
    33. }
    34. gvwGKWH.DataSource = dt;//绑定到数组
    35. gvwGKWH.DataBind();
    36. }
    37. #endregion 绑定GridView

推荐篇文章