C#利用递归方法在树控件中例遍磁盘目录及文件

/********
现在正在学习C#呆也无事就做了个资源管理器,这个是树控件列遍磁盘目录及文件的代码,其它的都很好实现,我就没有做,由于用的是递归很占系统内存,我也没有想出什么更好的方法,如果想让它快些只有在Mouse_LButton的Click事件里再加入我的算法,那样会更快些的,有兴趣的朋友可以把你更简单的算法帖出来,与大家一起分享!
*********/
///
///
///         作者:Leaf_KA      2005年7月11日
///
///
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ScanFileAndDirectory
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TreeView treeView1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  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.treeView1 = new System.Windows.Forms.TreeView();
   this.SuspendLayout();
   //
   // treeView1
   //
   this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.treeView1.ImageIndex = -1;
   this.treeView1.Location = new System.Drawing.Point(0, 0);
   this.treeView1.Name = "treeView1";
   this.treeView1.SelectedImageIndex = -1;
   this.treeView1.Size = new System.Drawing.Size(292, 273);
   this.treeView1.TabIndex = 0;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.treeView1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  private TreeNode[] tnRoot;
  private TreeNode[] tnSubRoot;
  private string[] strRoot;
  private string[] strSub;
  private string[] strTemp;
  private int Rnum;
  private int subNum;
  //这里有几个变量是没有用的,我也没有删,你们自己改一下吧
  private void Form1_Load(object sender, System.EventArgs e)
  {
   strRoot = Directory.GetLogicalDrives();
   Rnum = strRoot.Length; 
   GetRootNode();
  }
  private void GetRootNode(){
   tnRoot = new TreeNode[Rnum];
   try
   {
    for (int i = 0;i<Rnum;i++)
    {
     tnRoot[i] = new TreeNode(strRoot[i]);//根节点/磁盘
     this.treeView1.Nodes.Add(tnRoot[i]);//这里是写出所有磁盘    
     GetSubRootNode(tnRoot[i],strRoot[i]);//这里是列遍根目录下的子目录
     GetSubRootFile(tnRoot[i],strRoot[i]);//这里是列遍根目录下的文件
    }
   }
   catch(System.IO.IOException e){
    //因为有光盘,所以这里要加入错误处理
   }
  }
  string[] dirs;
  string[] myFiles;
  string tempStr;
  string tempStrFile;
  private void GetSubRootNode(TreeNode Root,string str)
  { //这里用的是递归方法,列遍所有目录
   
    dirs  = Directory.GetDirectories(str);
    foreach (string dir in dirs)
    { 
     tempStr=GetSubDirName(dir);
     TreeNode subRoot = new TreeNode(tempStr);
     
     GetSubRootNode(subRoot,dir);
     Root.Nodes.Add(subRoot);
     GetSubRootFile(subRoot,dir);  
    }
  }
  private string GetSubDirName(string str){
   //为了让在树里不显示全部的路径,在这里总是取出每个地址的最后一个目录名
   int iIndex=str.LastIndexOf(@"/");
   int iLen = str.Length;
   return str.Substring(iIndex+1);
  }
  private void GetSubRootFile(TreeNode Root,string str){
   //这里是列遍所有文件
   myFiles = Directory.GetFiles(str);
   foreach (string subFile in myFiles)
   {
    tempStrFile=GetSubDirName(subFile);
    Root.Nodes.Add(tempStrFile);    
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值