zoukankan      html  css  js  c++  java
  • 使用TreeView和listView控件查看本机磁盘文件夹及文件(WinForm)

    使用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;
       }
      }
     }
    }

     
     


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/juhengfei/archive/2008/09/18/2944997.aspx

  • 相关阅读:
    符瑞艺 160809228_C语言程序设计实验2 选择结构程序设计
    页面布局class常见命名规范
    CSS学习笔记
    HTML学习笔记
    虚拟机Centos7设置ip地址,并ping真机ip
    vue单页面开发和多页面开发的概念,及优缺点?
    传统的DOM渲染方式?
    面试题
    通过电脑chrome调试手机真机打开的微信H5页面,调试电脑微信H5页面(转载自 乐乐熊小妹)
    常见前端面试题及答案
  • 原文地址:https://www.cnblogs.com/runy/p/1533686.html
Copyright © 2011-2022 走看看