树行
来源:互联网 发布:出货单模板软件 编辑:程序博客网 时间:2024/06/06 02:08
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
namespace WindowsApplication5
{
/// <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.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
PopulateTree();
//
// 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();
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(24, 0);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(240, 216);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 240);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(124, 240);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(224, 240);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 278);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected void PopulateTree()
{
TreeNode tnRooNode;
treeView1.Nodes.Clear();
tnRooNode=new TreeNode(Environment.MachineName,2,2);
treeView1.Nodes.Add(tnRooNode);
TreeNodeCollection tncDrives=tnRooNode.Nodes;
string [] strLogicalDrives=Environment.GetLogicalDrives();
string strCurrentDrive="";
foreach(object obj in strLogicalDrives)
{
strCurrentDrive=(string )obj;
}
if(strCurrentDrive=="D://")
{
TreeNode tnDriveNode=new TreeNode(strCurrentDrive,3,3);
tncDrives.Add(tnDriveNode);
}
else
{
TreeNode tnDriveNode=new TreeNode(strCurrentDrive);
DirectoryInfo d=new DirectoryInfo(strCurrentDrive);
ShowDirectories(d,tncDrives,4,4);
}
}
public void ShowDirectories(DirectoryInfo d,TreeNodeCollection tncCollection ,int iImageIndex,int iSelectedImage)
{
TreeNode tnCurNode=new TreeNode(d.Name,iImageIndex,iSelectedImage);
tncCollection.Add(tnCurNode);
DirectoryInfo[]dirs;
try
{
dirs=d.GetDirectories();
}
catch(Exception)
{
return;
}
foreach(DirectoryInfo dir in dirs)
{
try
{
ShowDirectories(dir,tnCurNode.Nodes,0,1);
}
catch(UnauthorizedAccessException){
continue;
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Clipboard.SetDataObject(this.label1,true);
Application.Exit();
}
private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
StringBuilder str=new StringBuilder();
str.Insert(0,treeView1.SelectedNode.Text);
TreeNode tcNode=treeView1.SelectedNode;
while((tcNode=tcNode.Parent)!=null)
{
if(tcNode.Text.IndexOf('//')>=0)
{
string temp=tcNode.Text;
int index=temp.IndexOf('//');
temp.Remove(index,1);
str.Insert(0,temp);
}
if(tcNode.Text==Environment.MachineName)
break;
str.Insert(0,tcNode.Text+"//");
this.label1.Text=str.ToString();
}
}
}
}