treeView把文件夹作为根节点内容作为子节点
public partial class Form1 : Form { public Form1() { InitializeComponent(); TreeNode root = new TreeNode(); root.Text = "目录"; GetFiles(@"E:\Tools", root); treeView1.Nodes.Add(root); } private void GetFiles(string filePath, TreeNode node) { DirectoryInfo folder = new DirectoryInfo(filePath); node.Text = folder.Name; node.Tag = folder.FullName; FileInfo[] chldFiles = folder.GetFiles("*.*"); foreach (FileInfo chlFile in chldFiles) { TreeNode chldNode = new TreeNode(); chldNode.Text = chlFile.Name; chldNode.Tag = chlFile.FullName; node.Nodes.Add(chldNode); } DirectoryInfo[] chldFolders = folder.GetDirectories(); foreach (DirectoryInfo chldFolder in chldFolders) { TreeNode chldNode = new TreeNode(); chldNode.Text = folder.Name; chldNode.Tag = folder.FullName; node.Nodes.Add(chldNode); GetFiles(chldFolder.FullName, chldNode); } } }
三思而又行。