treeView显示目录及文件
完整代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Management;
using System.Text;
namespace test
{
/// <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.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(8, 16);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(272, 232);
this.treeView1.TabIndex = 0;
this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
this.treeView1.DoubleClick += new System.EventHandler(this.treeView1_DoubleClick);
this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
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 LoadDir loadDir;
string filter = "";
private void Form1_Load(object sender, System.EventArgs e)
{
loadDir = LoadDir.CreateIntance();
treeView1.Nodes.Add(loadDir.Drives());
}
private void treeView1_DoubleClick(object sender, System.EventArgs e)
{
if( treeView1.SelectedNode == null || treeView1.SelectedNode.Parent == null)//跟目录
return;
try
{
loadDir. ChildChildNodes(treeView1.SelectedNode,filter,true);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
if(treeView1.SelectedNode == null || treeView1.SelectedNode.Parent == null)//跟目录
return;
loadDir. ChildChildNodes(e.Node,filter,true);
}
private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
treeView1.SelectedNode = treeView1.GetNodeAt(e.X,e.Y);
}
}
/// <summary>
/// 加载文件目录,singleton
/// </summary>
public class LoadDir
{
private static LoadDir ld;
private LoadDir()
{
}
/// <summary>
/// 创建实例
/// </summary>
/// <returns>实例</returns>
public static LoadDir CreateIntance()
{
if(ld == null)
ld = new LoadDir();
return ld;
}
/// <summary>
/// 加载磁盘列表
/// </summary>
/// <returns>磁盘列表树结构</returns>
public TreeNode Drives()
{
TreeNode treeNode_myComp = new TreeNode("我的电脑",0,0);
SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk");
ManagementObjectSearcher searcher=new ManagementObjectSearcher(query);
try
{
foreach(ManagementBaseObject disk in searcher.Get())
{
if(Convert.ToInt32(disk["DriveType"]) == 3)
{
TreeNode tn = new TreeNode();
tn.Text = disk["VolumeName"].ToString()+"("+disk["Name"].ToString()+")";
tn.Tag = disk["Name"].ToString()+"\\";
treeNode_myComp.Nodes.Add(tn);
ChildNodes(tn,"",false);
}
}
}
catch
{}
return treeNode_myComp;
}
/// <summary>
/// 加载目录及文件
/// </summary>
/// <param name="selectedTreeNode"></param>
/// <param name="p_filter"></param>
/// <param name="Expand"></param>
/// <returns></returns>
public TreeNode ChildNodes(TreeNode selectedTreeNode,string p_filter,bool Expand)
{
if( File.Exists(selectedTreeNode.Tag.ToString()) != true )//目录而非文件
{
if( selectedTreeNode.GetNodeCount(false) <= 0 )//没打开过
{
try
{
foreach( string s in Directory.GetDirectories(selectedTreeNode.Tag.ToString()))
{
TreeNode tn = new TreeNode();
tn.Tag = s;
tn.Text = s.Split('\\')[(s.Split('\\').Length - 1)];
selectedTreeNode.Nodes.Add(tn);
}
foreach( string s in Directory.GetFiles(selectedTreeNode.Tag.ToString()))
{
TreeNode tn = new TreeNode();
tn.Tag = s;
tn.Text = s.Split('\\')[(s.Split('\\').Length - 1)];
selectedTreeNode.Nodes.Add(tn);
}
if(Expand == true)
selectedTreeNode.Expand();
}
catch
{
}
}
}
return selectedTreeNode;
}
/// <summary>
/// 取子级目录及文件
/// </summary>
/// <param name="selectedTreeNode"></param>
/// <param name="p_filter"></param>
/// <param name="Expand"></param>
/// <returns></returns>
public TreeNode ChildChildNodes(TreeNode selectedTreeNode,string p_filter,bool Expand)
{
try
{
foreach( TreeNode tn in selectedTreeNode.Nodes)
{
if( File.Exists(tn.Tag.ToString()) != true )//目录而非文件
{
ChildNodes(tn,p_filter,false);
}
}
}
catch
{
}
return selectedTreeNode;
}
public TreeNode ChildNodes(TreeNode selectedTreeNode)
{
return ChildNodes(selectedTreeNode,"",false);
}
}
}