1、先挂最顶级节点
TreeListNode pNode = tlFileDisplay.AppendNode(new object[] { beOpen.Text }, -1);
GetAllDirectory(beOpen.Text, tlFileDisplay, pNode);
函数:
/// <summary>
/// 遍历文件夹
/// </summary>
/// <param name="sPath">当前路径</param>
/// <param name="tlList">树结构</param>
/// <param name="pNodeParent">父节点</param>
private void GetAllDirectory(string sPath,TreeList tlList,TreeListNode pNodeParent)
{
string[] sDirectory = Directory.GetDirectories(sPath);
foreach (string sDirPath in sDirectory)
{
TreeListNode pNode = tlFileDisplay.AppendNode(new object[] { sDirPath }, pNodeParent);
GetAllFiles(sDirPath, tlList, pNode);
GetAllDirectory(sDirPath, tlList, pNode);
}
}
/// <summary>
/// 遍历文件
/// </summary>
/// <param name="sPath">路径</param>
/// <param name="TreeList">树结构</param>
/// <param name="ParentNode">父节点</param>
private void GetAllFiles(string sPath, TreeList TreeList, TreeListNode ParentNode)
{
string[] sFiles = Directory.GetFiles(sPath);
foreach (string sFileName in sFiles)
{
TreeList.AppendNode(new object[] { sFileName }, ParentNode);
}
}