/*
因为收集了许多C#编程方面的小技巧,为了便于查看,便做了这个小程序
*/
//操作TreeView,生成文件目录树
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections;
namespace codeView
{
/// <summary>
/// loadFiles 的摘要说明。
/// </summary>
public class loadFiles
{
public loadFiles()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private TreeView tv1;
public void loadToTree(TreeView tv)
{
this.tv1=tv;
TreeNode fatherNode=new TreeNode(Application.StartupPath);
this.tv1.Nodes.Add(fatherNode);
this.getAllDirectories(new DirectoryInfo(Application.StartupPath),fatherNode);
}
private void getAllDirectories(DirectoryInfo info1,TreeNode nd)
{
DirectoryInfo[] infoArray1 = info1.GetDirectories();
foreach(DirectoryInfo dir in infoArray1)
{
TreeNode node=new TreeNode(dir.Name);
FileInfo[] arrfiles=dir.GetFiles();
for (int num2 = 0; num2 < arrfiles.Length; num2++)
{
FileInfo info4 = arrfiles[num2];
TreeNode fileNode=new TreeNode(info4.Name);
node.Nodes.Add(fileNode);
}
this.getAllDirectories(dir,node);
//通过递归搜索出该路径下所有文件
nd.Nodes.Add(node);
}
}
}
}
//主界面
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
namespace codeView
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeViewCode;
private System.Windows.Forms.Button btOpen;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// 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.treeViewCode = new System.Windows.Forms.TreeView();
this.btOpen = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeViewCode
//
this.treeViewCode.ImageIndex = -1;
this.treeViewCode.Location = new System.Drawing.Point(8, 16);
this.treeViewCode.Name = "treeViewCode";
this.treeViewCode.SelectedImageIndex = -1;
this.treeViewCode.Size = new System.Drawing.Size(424, 416);
this.treeViewCode.TabIndex = 0;
this.treeViewCode.DoubleClick += new System.EventHandler(this.treeViewCode_DoubleClick);
//
// btOpen
//
this.btOpen.Location = new System.Drawing.Point(440, 64);
this.btOpen.Name = "btOpen";
this.btOpen.Size = new System.Drawing.Size(80, 32);
this.btOpen.TabIndex = 1;
this.btOpen.Text = "打开";
this.btOpen.Click += new System.EventHandler(this.btOpen_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 470);
this.Controls.Add(this.btOpen);
this.Controls.Add(this.treeViewCode);
this.Name = "frmMain";
this.Text = "查看源代码";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void treeViewCode_DoubleClick(object sender, System.EventArgs e)
{
this.openFile();
}
private loadFiles loadF;
private void frmMain_Load(object sender, System.EventArgs e)
{
loadF=new loadFiles();
loadF.loadToTree(this.treeViewCode);
}
private void btOpen_Click(object sender, System.EventArgs e)
{
this.openFile();
}
private void openFile()
{
if(this.treeViewCode.SelectedNode.Parent!=null)
{
string fileName =this.treeViewCode.SelectedNode.Text;
TreeNode nd=this.treeViewCode.SelectedNode;
string fileFullName = Application.StartupPath;
MessageBox.Show(nd.FullPath);
Process.Start(nd.FullPath);
}
}
}
}