using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace LoopBulidTree
{
public partial class Form1 : Form
{
#region 1: 初始化定义数据
string aimFilePath = @"我的电脑\C:\Documents and Settings\foven\桌面\File Test";
int arraySymbol = 1; //初始化层数
object[][] sumArray = new object[20][]; // 综合各层文件夹的 FullName
ArrayList sonList = new ArrayList(); //存放一层的子文件夹的地址;
#endregion
public Form1()
{
InitializeComponent();
BulidRoot();//初始化节点列表
for (int i = 1; i < 8;i++ )
{
SetParentNode(sumArray[i],false);
}
}
public string GetFileName(string str) // (从完整路径) 取出文件夹的名称
{
string name = str.Remove(0, (str.LastIndexOf(@"\") + 1));
return name;
} public void BulidRoot() // 建立 根节点
{
sumArray[0] = new object[] { aimFilePath };
TreeNode rootNode = new TreeNode(GetFileName(aimFilePath));
treeView1.Nodes.Add(aimFilePath);
SetParentNode(sumArray[0],true);
} public void SetNode_LayerArray(object[] sonNodeArray) //节点_层次 二维数组
{
sumArray [arraySymbol] = sonNodeArray;
arraySymbol++; // 本层已完 换下层
sonList.Clear();
} public void SetParentNode(object[] parentArray, bool rootPathJudge)
{
for (int i = 0; i < parentArray.Length; i++)
{
bool loopFinish;
string parentPath = parentArray[i].ToString();
if (rootPathJudge)
{ parentPath = parentPath.Remove(0, 5); }
TreeNode pNode = new TreeNode(parentPath); //问题好想出在这里 是新建了一个节点 而不是取出已添加的节点 我想不出办法了
if (i == parentArray.Length - 1) { loopFinish = true; }
else { loopFinish = false; }
AddSonNode(parentPath, pNode, loopFinish);
}
}
public void AddSonNode(string upPath,TreeNode tn,bool flag)
{
tn.Nodes.Clear();
//获得当前目录
DirectoryInfo dirinfo = new DirectoryInfo(upPath);
DirectoryInfo[] adirinfo;
try
{
adirinfo = dirinfo.GetDirectories();
}
catch
{ return; }
foreach (DirectoryInfo di in adirinfo)
{
TreeNode tnDir = new TreeNode(di.Name, 4, 5);
tn.Nodes.Add(tnDir);
sonList.Add(di.FullName);
}
if (flag) // 本层父节点扫描完毕
{
object[] sonArry = sonList.ToArray();
SetNode_LayerArray(sonArry);
}
}
}
} VS 2005 C# 里面建个应用程序 加个Treeview控件 这个代码就能运行了吧。 我想完成的是 输入一个文件夹的 完全路径 “string aimFilePath = @"我的电脑\C:\Documents and Settings\foven\桌面\File Test";” 然后把这个文件夹的所有子文件夹以树形结构显示在 Treeview 控件上。有些类似与 资源管理器 但不同的是,这里运行后会自动展开全部节点,而不需要去点击各个节点去展开。
缠了我几天了,麻烦帮我看下
万分感谢!
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace LoopBulidTree
{
public partial class Form1 : Form
{
#region 1: 初始化定义数据
string aimFilePath = @"我的电脑\C:\Documents and Settings\foven\桌面\File Test";
int arraySymbol = 1; //初始化层数
object[][] sumArray = new object[20][]; // 综合各层文件夹的 FullName
ArrayList sonList = new ArrayList(); //存放一层的子文件夹的地址;
#endregion
public Form1()
{
InitializeComponent();
BulidRoot();//初始化节点列表
for (int i = 1; i < 8;i++ )
{
SetParentNode(sumArray[i],false);
}
}
public string GetFileName(string str) // (从完整路径) 取出文件夹的名称
{
string name = str.Remove(0, (str.LastIndexOf(@"\") + 1));
return name;
} public void BulidRoot() // 建立 根节点
{
sumArray[0] = new object[] { aimFilePath };
TreeNode rootNode = new TreeNode(GetFileName(aimFilePath));
treeView1.Nodes.Add(aimFilePath);
SetParentNode(sumArray[0],true);
} public void SetNode_LayerArray(object[] sonNodeArray) //节点_层次 二维数组
{
sumArray [arraySymbol] = sonNodeArray;
arraySymbol++; // 本层已完 换下层
sonList.Clear();
} public void SetParentNode(object[] parentArray, bool rootPathJudge)
{
for (int i = 0; i < parentArray.Length; i++)
{
bool loopFinish;
string parentPath = parentArray[i].ToString();
if (rootPathJudge)
{ parentPath = parentPath.Remove(0, 5); }
TreeNode pNode = new TreeNode(parentPath); //问题好想出在这里 是新建了一个节点 而不是取出已添加的节点 我想不出办法了
if (i == parentArray.Length - 1) { loopFinish = true; }
else { loopFinish = false; }
AddSonNode(parentPath, pNode, loopFinish);
}
}
public void AddSonNode(string upPath,TreeNode tn,bool flag)
{
tn.Nodes.Clear();
//获得当前目录
DirectoryInfo dirinfo = new DirectoryInfo(upPath);
DirectoryInfo[] adirinfo;
try
{
adirinfo = dirinfo.GetDirectories();
}
catch
{ return; }
foreach (DirectoryInfo di in adirinfo)
{
TreeNode tnDir = new TreeNode(di.Name, 4, 5);
tn.Nodes.Add(tnDir);
sonList.Add(di.FullName);
}
if (flag) // 本层父节点扫描完毕
{
object[] sonArry = sonList.ToArray();
SetNode_LayerArray(sonArry);
}
}
}
} VS 2005 C# 里面建个应用程序 加个Treeview控件 这个代码就能运行了吧。 我想完成的是 输入一个文件夹的 完全路径 “string aimFilePath = @"我的电脑\C:\Documents and Settings\foven\桌面\File Test";” 然后把这个文件夹的所有子文件夹以树形结构显示在 Treeview 控件上。有些类似与 资源管理器 但不同的是,这里运行后会自动展开全部节点,而不需要去点击各个节点去展开。
缠了我几天了,麻烦帮我看下
万分感谢!
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(textBox1.Text);
if (!di.Exists)
{
MessageBox.Show("路径不存在");
return;
}
TreeNode tn = new TreeNode(di.Name);
tn.Tag = di.FullName;
treeView1.Nodes.Add(tn);
AddChildNodes(tn);
treeView1.ExpandAll();
} private void AddChildNodes(TreeNode tn)
{
DirectoryInfo di = new DirectoryInfo(tn.Tag.ToString());
if (!di.Exists)
return;
foreach (DirectoryInfo dir in di.GetDirectories("*.*", SearchOption.AllDirectories))
{
TreeNode node = new TreeNode(dir.Name);
node.Tag = dir.FullName;
tn.Nodes.Add(node);
AddChildNodes(node);
}
}
EnumServerDirectories(TvServerDirectories.SelectedNode);#region DataTable GetServerDirectories(String ParentPath) // 取子目录
/// <summary>
/// 取子目录
/// </summary>
/// <param name="ParentPath">父附录</param>
/// <returns>子目录</returns>
DataTable GetServerDirectories(String ParentPath)
{
using (SqlConnection Connection = new SqlConnection(ConnectionString))
{
SqlDataAdapter DataAdapter = new SqlDataAdapter("MASTER..XP_SUBDIRS '" + ParentPath + "'", Connection);
DataTable DT = new DataTable();
DataAdapter.Fill(DT);
return DT;
}
}
#endregion #region void EnumServerDirectories(TreeNode ParentNode) // 加载目录
/// <summary>
/// 加载目录
/// </summary>
/// <param name="ParentNode">父节点</param>
void EnumServerDirectories(TreeNode ParentNode)
{
TvServerDirectories.SelectedNode = ParentNode;
String DirectoryPath = ParentNode.Tag.ToString();
if (ParentNode.Nodes.Count == 0)
{
if (DirectoryPath.Substring(DirectoryPath.Length - 1) != "\\")
DirectoryPath += "\\";
try
{
foreach (DataRow DR in GetServerDirectories(DirectoryPath).Rows)
{
TreeNode TempNode = new TreeNode();
TempNode.Text = DR["subDirectory"].ToString();
TempNode.Tag = DirectoryPath + DR["subDirectory"].ToString() + "\\";
TempNode.ImageIndex = 3;
TempNode.SelectedImageIndex = 2;
TvServerDirectories.SelectedNode.Nodes.Add(TempNode);
}
}
catch (Exception)
{
}
}
}
#endregion
foreach (DataRow DR in GetServerDirectories(DirectoryPath).Rows)
{
TreeNode TempNode = new TreeNode();
TempNode.Text = DR["subDirectory"].ToString();
TempNode.Tag = DirectoryPath + DR["subDirectory"].ToString() + "\\";
TempNode.ImageIndex = 3;
TempNode.SelectedImageIndex = 2;
TvServerDirectories.SelectedNode.Nodes.Add(TempNode);
EnumServerDirectories(TempNode); // 递归调用
}
{
DirectoryInfo di = new DirectoryInfo(textBox1.Text);
if (!di.Exists)
{
MessageBox.Show("路径不存在");
return;
}
TreeNode tn = new TreeNode(di.Name);
tn.Tag = di.FullName;
treeView1.Nodes.Add(tn);
AddChildNodes(tn);
treeView1.ExpandAll();
} private void AddChildNodes(TreeNode tn)
{
DirectoryInfo di = new DirectoryInfo(tn.Tag.ToString());
if (!di.Exists)
return;
foreach (DirectoryInfo dir in di.GetDirectories("*.*", SearchOption.AllDirectories))
{
TreeNode node = new TreeNode(dir.Name);
node.Tag = dir.FullName;
tn.Nodes.Add(node);
AddChildNodes(node);
}
}
呵呵 在次感谢 ojlovecd 提供的方法! 搞定了 结贴!