/********
现在正在学习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);
}
}
}
}