Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)

时间:2022-05-24 23:34:04

场景

Winform中DevExpress的TreeList的入门使用教程(附源码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100075677

https://www.cnblogs.com/badaoliumangqizhi/p/11412053.html

在上面实现给TreeList赋值的基础上,将其数据源更改为本地某路径下的所有文件和目录。

效果

Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)

Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)

实现

在原来的节点类中添加节点类型属性,该属性是枚举类型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DevExpressSimple
{
class TreeNode
{
//标识Id
private string id;
//父级节点ID
private string parentId;
//节点显示文本
private string nodeText;
private TreeNodeTypes nodeType = TreeNodeTypes.Folder; public TreeNodeTypes NodeType
{
get { return nodeType; }
set { nodeType = value; }
} public string NodeText
{
get { return nodeText; }
set { nodeText = value; }
}
public string ParentId
{
get { return parentId; }
set { parentId = value; }
} public string Id
{
get { return id; }
set { id = value; }
}
}
}

然后新建枚举类TreeNodeType

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DevExpressSimple
{
public enum TreeNodeTypes
{
/// <summary>
/// 文件夹
/// </summary>
Folder = ,
/// <summary>
/// 文件
/// </summary>
File =
}
}

然后新建工具类TreeListHelper,用来将目录转换成节点对象并添加到数据源。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DevExpressSimple
{
class TreeListHelper
{
public static List<TreeNode> ParseDir(string dataRootDir, List<TreeNode> data)
{
//如果传递的list为空,则新建一个
if (data == null)
{
data = new List<TreeNode>();
}
//如果目录不存在则直接原样将data返回
if (!System.IO.Directory.Exists(dataRootDir))
{
return data;
} TreeNode node = null;
//创建目录对象
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(dataRootDir);
//使用数组接收目录下的所有目录
System.IO.DirectoryInfo[] subDirs = dir.GetDirectories();
//循环每个目录
foreach (System.IO.DirectoryInfo subDir in subDirs)
{
//新建节点对象
node = new TreeNode();
//节点的Id取当前目录的名字
node.Id = subDir.Name;
//节点的父级ID 取上层目录的名字
node.ParentId = dir.Name;
//节点要显示的文本也取当前目录的名字
node.NodeText = subDir.Name;
//节点类型为文件夹
node.NodeType = TreeNodeTypes.Folder;
//将当前节点添加到list数据源
data.Add(node);
//因为是文件夹,所以需要迭代当前方法 并将当前目录作为参数重新传递 直到迭代完所有文件夹
//这里使用FullName方法 获取全路径
ParseDir(subDir.FullName, data);
}
//遍历完文件夹之后 遍历 文件
//使用FileInfo的GetGiles方法 获取所有文件
System.IO.FileInfo[] subFiles = dir.GetFiles();
//遍历所有文件
foreach (System.IO.FileInfo subFile in subFiles)
{
node = new TreeNode();
node.Id = subFile.Name;
node.ParentId = dir.Name;
node.NodeText = subFile.Name;
node.NodeType = TreeNodeTypes.File;
data.Add(node);
}
//返回数据源
return data;
}
}
}

再回到原来窗体加载的代码中设置数据源list的地方。

   //新建list数据源
List<TreeNode> data = new List<TreeNode>();
//data.Add(new TreeNode() { Id = "root", ParentId = String.Empty, NodeText = "测试1" });
// data.Add(new TreeNode() { Id = "first", ParentId = "root", NodeText = "测试2" });
//将指定目录下的所有文件以及文件夹封装成节点对象并添加到list
data = TreeListHelper.ParseDir(@"E:\test", data);
//添加根节点 Id属性对应根目录的名字, 父级Id为空 节点显示的文本 为 “所有文本” 节点类型为 文件夹
data.Add(new TreeNode() { Id = "test", ParentId = String.Empty, NodeText = "所有文件", NodeType = TreeNodeTypes.Folder });

完整示例代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace DevExpressSimple
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ string keyFieldName = "Id";
string parentFieldName = "ParentId";
//新建list数据源
List<TreeNode> data = new List<TreeNode>();
//data.Add(new TreeNode() { Id = "root", ParentId = String.Empty, NodeText = "测试1" });
// data.Add(new TreeNode() { Id = "first", ParentId = "root", NodeText = "测试2" });
//将指定目录下的所有文件以及文件夹封装成节点对象并添加到list
data = TreeListHelper.ParseDir(@"E:\test", data);
//添加根节点 Id属性对应根目录的名字, 父级Id为空 节点显示的文本 为 “所有文本” 节点类型为 文件夹
data.Add(new TreeNode() { Id = "test", ParentId = String.Empty, NodeText = "所有文件", NodeType = TreeNodeTypes.Folder });
//添加单列
DevExpress.XtraTreeList.Columns.TreeListColumn colNode = new DevExpress.XtraTreeList.Columns.TreeListColumn();
//设置名字
colNode.Name = "名字";
//设置标题
colNode.Caption = "标题";
//设置从数据源分配给当前列的字段名。
colNode.FieldName = "NodeText";
//设置树列表中显示当前列的位置。
colNode.VisibleIndex = ;
//是否可见
colNode.Visible = true;
//是否允许编辑
colNode.OptionsColumn.AllowEdit = false;
//是否允许移动
colNode.OptionsColumn.AllowMove = false;
//是否允许移动至自定义窗体
colNode.OptionsColumn.AllowMoveToCustomizationForm = false;
//是否允许排序
colNode.OptionsColumn.AllowSort = false;
//是否固定列宽
colNode.OptionsColumn.FixedWidth = false;
//是否只读
colNode.OptionsColumn.ReadOnly = true;
//移除列后是否允许在自定义窗体中显示
colNode.OptionsColumn.ShowInCustomizationForm = true;
//先清除列
this.treeList1.Columns.Clear();
//将列数组添加到集合的结尾。
this.treeList1.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { colNode });
this.treeList1.OptionsView.ShowColumns = false; //隐藏列标头
this.treeList1.OptionsView.ShowIndicator = false; //隐藏节点指示器面板 this.treeList1.OptionsView.ShowHorzLines = false; //隐藏水平表格线
this.treeList1.OptionsView.ShowVertLines = false; //隐藏垂直表格线
this.treeList1.OptionsView.ShowIndentAsRowStyle = false;
#region 绑定数据源
//设置属性KeyFieldName ParentFieldName
//设置一个值,该值指定绑定到XtratreeList控件的数据源的键字段
this.treeList1.KeyFieldName = keyFieldName;
//设置一个值,该值表示标识此数据源中父记录的数据源字段。
this.treeList1.ParentFieldName = parentFieldName;
this.treeList1.DataSource = data;
//刷新数据
this.treeList1.RefreshDataSource(); #endregion }
}
}

这里指定的目录是E盘下的test目录,在test下新建一些目录和文件。

Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)
 Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)

示例源码下载

https://download.csdn.net/download/badao_liumang_qizhi/11614756