|
发表于 2017-6-3 14:39:15
|
显示全部楼层
广西壮族自治区柳州市
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- //打开目录
- FolderBrowserDialog fbd = new FolderBrowserDialog();
- fbd.ShowDialog();
- //添加目录名称作为主节点
- string path = Path.GetFileName(fbd.SelectedPath);
- if (path == String.Empty)
- {
- //说明打开的是盘符
- TreeNode tv1 = treeView1.Nodes.Add(fbd.SelectedPath);
- LoadDate(tv1, fbd.SelectedPath);
- }
- else
- {
- TreeNode tv2 = treeView1.Nodes.Add("111");
- LoadDate(tv2, fbd.SelectedPath);
- }
- }
- catch
- {
- }
- }
- public static void LoadDate(TreeNode tnc, string path)
- {
- try
- {
- DirectoryInfo di = new DirectoryInfo(path);
- DirectoryInfo[] folder = di.GetDirectories();
- FileInfo[] file = di.GetFiles("*.txt");
- foreach (DirectoryInfo item in folder)
- {
- TreeNode Node = tnc.Nodes.Add(item.Name);
- LoadDate(Node, item.FullName);
- }
- foreach (var item in file)
- {
- TreeNode tn = tnc.Nodes.Add(Path.GetFileName(item.Name));
- tn.Tag = path + "\" + item.Name;
- }
- }
- catch
- {
- }
- }
复制代码
哈哈这是我以前写的 添加到树节点一级级目录 |
|