你可以去看看一些基础知识,也就是方法自己调用自己,如下面的方法:
private void a()
{
a();
}
public void RecureTreeView(TreeNodeCollection nodes){
foreach(TreeNode node in nodes){
RecureTreeView(node.Nodes);
Debug.WriteLine(node.Text);
}
}
递归就是自己调用自己,自己写一个方法体,在方法体里面再次调用自己就ok了。
void dg1(string path,TreeNode tn)
{
string[] str = Directory.GetDirectories(path);
foreach (string s in str)
{
TreeNode Tn=tn.Nodes.Add(Path.GetFileName(s));
dg1(s, Tn);
}
string[] file = Directory.GetFiles(path);
foreach (string s1 in file)
{
tn.Nodes.Add(Path.GetFileName(s1));
}
}
private void CreateChildNode(TreeNodeCollection nodes, DataTable dt, int parentId)
{
string selectId = string.Format("FatherId={0}", parentId);
DataRow[] rowList = dt.Select(selectId);
foreach (DataRow dr in rowList)
{
TreeNode tn = new TreeNode();
nodes.Add(tn);
tn.Text = dr["PowerName"].ToString();
tn.Value = dr["Id"].ToString();
tn.Expanded = false;
//tn.ImageUrl = "~/SystemSet/Image/Root.gif";
tn.NavigateUrl = dr["LinkUrl"].ToString();
tn.Target = "I1";
int id = int.Parse(tn.Value);
// node.ChildNodes.Add(tn);
CreateChildNode(tn.ChildNodes, dt, id);
dt.Rows.Remove(dr);