一个人的天空

知识就像宇宙,你不知道它到底存在多少奥秘,必须不断的探索
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
使用TreeView和listView控件查看本机磁盘文件夹及文件
在页面上放TreeView,listView,splitter,imageList 四个控件,在imageList中添加两个小图片,然后设置一下
treeview控件的checkboxex=true,Dock=left,ImageList=ImageList1(控件名)ImageIndex=1,SelectedImageIndex=0(这两个属性的值是根据你的ImageList控件添加的图片的索引决定的),
showLines=false;
代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace NODE
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TreeView treeView1;
  private System.Windows.Forms.ImageList imageList1;
  private System.Windows.Forms.Splitter splitter1;
  private System.Windows.Forms.ListView listView1;
  private System.ComponentModel.IContainer components;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.treeView1 = new System.Windows.Forms.TreeView();
   this.imageList1 = new System.Windows.Forms.ImageList(this.components);
   this.splitter1 = new System.Windows.Forms.Splitter();
   this.listView1 = new System.Windows.Forms.ListView();
   this.SuspendLayout();
   //
   // treeView1
   //
   this.treeView1.CheckBoxes = true;
   this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
   this.treeView1.ImageIndex = 1;
   this.treeView1.ImageList = this.imageList1;
   this.treeView1.Location = new System.Drawing.Point(0, 0);
   this.treeView1.Name = "treeView1";
   this.treeView1.ShowLines = false;
   this.treeView1.Size = new System.Drawing.Size(152, 301);
   this.treeView1.TabIndex = 0;
   this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
   this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
   //
   // imageList1
   //
   this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
   this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
   this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
   //
   // splitter1
   //
   this.splitter1.Location = new System.Drawing.Point(152, 0);
   this.splitter1.Name = "splitter1";
   this.splitter1.Size = new System.Drawing.Size(3, 301);
   this.splitter1.TabIndex = 1;
   this.splitter1.TabStop = false;
   //
   // listView1
   //
   this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.listView1.Location = new System.Drawing.Point(155, 0);
   this.listView1.Name = "listView1";
   this.listView1.Size = new System.Drawing.Size(413, 301);
   this.listView1.SmallImageList = this.imageList1;
   this.listView1.TabIndex = 2;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(568, 301);
   this.Controls.Add(this.listView1);
   this.Controls.Add(this.splitter1);
   this.Controls.Add(this.treeView1);
   this.Name = "Form1";
   this.Text = "使用TreeView和listView控件查看本机磁盘文件夹及文件";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   string[]drives=Directory.GetLogicalDrives();//得到本机上的驱动器
   foreach(string drive in drives)//循环
   {
    MyNode mn=new MyNode(drive,true);
    this.treeView1.Nodes.Add(mn);//添加驱动器名到TREEVIEW控件上
   }

   this.listView1.Columns.Add("名称",this.listView1.Width/4,HorizontalAlignment.Center);
   this.listView1.Columns.Add("大小",this.listView1.Width/4,HorizontalAlignment.Center);
   this.listView1.Columns.Add("类型",this.listView1.Width/4,HorizontalAlignment.Center);
   this.listView1.Columns.Add("修改时间",this.listView1.Width/4,HorizontalAlignment.Center);
   this.listView1.View=View.Details;
  }

  private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)//TreeView选中之后
  {
   try
   {
    MyNode mn=(MyNode)e.Node;
    if(mn.isLoadFiles==false && mn.Nodes.Count==0 )
    {
     DirectoryInfo di=new DirectoryInfo(mn.MyPath);
     this.listView1.Items.Clear();
     foreach(DirectoryInfo d in di.GetDirectories())//如果为文件夹
     {
      mn.Nodes.Add(new MyNode(d.FullName,false));
      ListViewItem lvi =this.listView1.Items.Add(d.Name);
      lvi.SubItems.Add("");
      lvi.SubItems.Add("文件夹");
      lvi.SubItems.Add(d.LastAccessTime.ToString());
      lvi.ImageIndex=1;//设置图标
     }
     foreach(FileInfo f in di.GetFiles())//如果为文件
     {
      
      ListViewItem lvi=this.listView1.Items.Add(f.Name,0);//加载文件名及图标
      lvi.SubItems.Add(f.Length.ToString());
      lvi.SubItems.Add("文件");
      lvi.SubItems.Add(f.LastAccessTime.ToString());
     }
    }
   }
   catch
   {
   }
   
  }

  private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
  {
//   TreeNode tnx=e.Node;//获取选中的树节点
//   foreach(TreeNode t in tnx.Nodes)//循环这个节点下的子节点
//   {
//    t.Checked=e.Node.Checked;//选中所有的子节点
//   }
   
   //上下两段代码可以任选其一测试

   bool isChecked=true;//定义一个BOOL值
   
   TreeNode parentNode=e.Node.Parent;//得到当前选中的节点的父节点
   if(parentNode==null)//如果没有父节点 则返回
    return;
   TreeNode tn=parentNode.FirstNode;//得到父节点的的一个节点
   while(tn!=null)//如果不为空
   {
    if(tn.Checked==false)//如果此节点没有选中
    {
     isChecked=false;//BOOL为FALSE
     break;//跳出
    }
    tn=tn.NextNode;//移至下一个节点
   }
   parentNode.Checked=isChecked;//根据BOOL值判断父节点是否选中
  }
 }

 public class MyNode:TreeNode//一个继承TreeNode的类
 {
  private string mytext=null;
  public bool isLoadFiles=false;
  public MyNode(string text,bool isRoot)
  {
   mytext=text;
   if(isRoot)//这里执行是查找本机驱动器的时候执行
   {
    base.Text=text.Substring(0,text.LastIndexOf("\\")-1);
   }
   else//这里执行是查找本机文件夹以及文件的时候执行
   {
    base.Text=text.Substring(text.LastIndexOf("\\")+1);
   }
  }
  public string MyPath
  {
   get
   {
    return mytext;
   }
  }
 }
}