using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace C11_资源管理器
{
public partial class Form1 : Form
{

string path = @"d:\广州传智";

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//得到根目录下的所有的子目录的路径
string[] dirs = Directory.GetDirectories(path);
//TreeView下的根节点在TreeView对象的Nodes集合中.
foreach (string dir in dirs)
{
//每1个节点的类型是1个TreeNode类型
//TreeNode对象的Text属性就是 节点上面显示的文本.
TreeNode node = new TreeNode();
//将全路径保存在node对象的tag属性中.
node.Tag = dir;
node.Text = Path.GetFileName(dir);
tvMain.Nodes.Add(node);
//tvMain.Nodes.Add(Path.GetFileName(dir));
}

}

/// <summary>
/// 在选中1个节点之后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvMain_AfterSelect(object sender, TreeViewEventArgs e)
{
//0. 清空ListView中的所有的项目
lvData.Items.Clear();
//1. 得到选中的节点
string path = tvMain.SelectedNode.Tag.ToString();
//2.得到选中的节点所代表的文件夹的路径
//3. 将这个路径下的所有的文件的信息读出.
//将路径下面的所有的文件的信息返回.
string[] files = Directory.GetFiles(path);
ListViewItem item = null;
//遍历files数组 将每1个元素封装为1个ListViewItem对象 并添加到ListView中
FileInfo info = null;
foreach (string file in files)
{
info = new FileInfo(file);
item = new ListViewItem();
item.Text = Path.GetFileName(file);
item.Tag = file;
item.SubItems.Add(info.Length.ToString());
item.SubItems.Add(info.CreationTime.ToString());
lvData.Items.Add(item);
}
//加载当前目录下的子文件夹为当前节点的子节点.
string[] dirs = Directory.GetDirectories(path);
foreach (string str in dirs)
{
TreeNode node = new TreeNode();
node.Text = Path.GetFileName(str);
node.Tag = str;
tvMain.SelectedNode.Nodes.Add(node);
}

}

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//1. 得到我们选中的那1项吧
string path = lvData.SelectedItems[0].Tag.ToString();
//2. 得到选中的那1项的文件的全路径
if (MessageBox.Show("你真的要删除吗?删除以后就找不到了哦!", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)== System.Windows.Forms.DialogResult.Yes)
{
// //3.删除文件.
File.Delete(path);
lvData.SelectedItems[0].Remove();
}
}
}
}

递归生成treeview树形节点(没有用递归函数之后会有补充,这里只用系统的内置方法去生成)的更多相关文章

  1. Python之旅Day3 文件操作 函数(递归|匿名|嵌套|高阶)函数式编程 内置方法

    知识回顾 常见五大数据类型分类小结:数字.字符串.列表.元组.字典 按存值个数区分:容器类型(列表.字典.元组) 标量原子(数字.字符串) 按是否可变区分:可变(列表.字典) 不可变(数字.字符串.元 ...

  2. python递归和内置方法

    递归:函数调用自身 核心:递进的时候能够达到一个结果,问题规模越来越小(不一定要真正的达到):设置一个条件,能够让最后一次函数调用结束 练习: ​ 第一个人的姓名是16岁,后面每个人的年龄都比前一个大 ...

  3. 递归,装饰器,python常用内置方法

    **递归**        def calc(n):            print(n)            if int(n / 2) == 0:  条件判断                r ...

  4. C# winform 递归选中TreeView子节点

    /// <summary> /// 递归选中所有的自节点 /// </summary> /// <param name="nodeThis">T ...

  5. swift 4 生成随机数的内置方法汇总

    第一种是drand48(),不接收参数, 返回的类型是Double. 就返回 0到1之间的Double类型的随机数.举个例子: //每次点击button,button 的颜色会随机变换. class ...

  6. 简学Python第三章__函数式编程、递归、内置函数

    #cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...

  7. day 14 三元运算符,列表字典推导式,递归,匿名函数,内置函数(排序,映射,过滤,合并)

    一.三元运算符 就是if.....else..... 语法糖 前提:if和else只有一条语句 #原始版 cmd=input('cmd') if cmd.isdigit(): print('1') e ...

  8. Python基础3 函数、递归、内置函数

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  9. python学习笔记:第14天 内置函数补充和递归

    一.匿名函数 匿名函数主要是为了解决一些简单需求而设计的一种函数,匿名函数的语法为: lambda 形参: 返回值 先来看一个例子: # 计算n的n次方 In[2]: lst = lambda n: ...

随机推荐

  1. 0_Simple__simpleSurfaceWrite

    使用表面写入函数,结合纹理引用实现图片的旋转▶ 源代码 #include <stdio.h> #include <windows.h> #include <cuda_ru ...

  2. java二维数组的长度

    //多少行 a.length //多少列 a[i].length

  3. JS的prototype和__proto__、constructor

    看了JS的prototype和__proto__这篇文章,才感觉很清晰了,对于原型这块,以前经常把这些属性弄不清楚, 明白了之后保存下整理下: prototype: 是函数的一个属性(每个函数都有一个 ...

  4. can't load package the specified module could not be found

    can't load package the specified module could not be found 用 Dependency Walker 2.2Dependency Walker ...

  5. Mysql 索引优化 - 2

    永远小表驱动大表(小数据驱动大数据) in exists区别, SELECT * FROM A WHERE A.id in (SELECT id FORM B) 若A表数据大于B表数据用in SELE ...

  6. Posting JSON to Spring MVC Controller

    Spring MVC can be setup to automatically bind incoming JSON string into a Java object. Firstly, ensu ...

  7. 从底层谈WebGIS 原理设计与实现(一):开篇

    从底层谈WebGIS 原理设计与实现(一):开篇 作者:naaoveGI…    文章来源:http://www.cnblogs.com/naaoveGIS/    点击数:4773    更新时间: ...

  8. 获取tomcat源码

    1.安装环境 工欲善其事必先利其器,在开始工作之前,先准备好环境和工具:jdk.eclipse.并配置eclipse的svn插件.ant,具体怎么配置就不说了,网上都有. 2.下载源码 打开eclip ...

  9. sqlserver查询自定义的函数

    1)sp_helptext同样适应用自定义函数 2)sys.sql_modules表也可以查 查看函数的源代码: exec sp_helptext '函数名'

  10. python-最好大学排名

    # -*- coding: utf-8 -*-"""Created on Mon Apr 3 09:37:52 2017 @author: zuihaodaxuepaim ...