using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
namespace WindowsApplication15
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnGo;
private System.Windows.Forms.TextBox txtPath;
private System.Windows.Forms.TreeView treeView;
/// <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.btnGo = new System.Windows.Forms.Button();
this.txtPath = new System.Windows.Forms.TextBox();
this.treeView = new System.Windows.Forms.TreeView();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.SuspendLayout();
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(248, 8);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(32, 20);
this.btnGo.TabIndex = 0;
this.btnGo.Text = "Go";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// txtPath
//
this.txtPath.Location = new System.Drawing.Point(8, 8);
this.txtPath.Name = "txtPath";
this.txtPath.Size = new System.Drawing.Size(232, 20);
this.txtPath.TabIndex = 1;
this.txtPath.Text = "";
//
// treeView
//
this.treeView.ImageIndex = -1;
this.treeView.Location = new System.Drawing.Point(8, 48);
this.treeView.Name = "treeView";
this.treeView.SelectedImageIndex = -1;
this.treeView.Size = new System.Drawing.Size(272, 216);
this.treeView.TabIndex = 2;
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(8, 32);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(272, 8);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(288, 273);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.treeView);
this.Controls.Add(this.txtPath);
this.Controls.Add(this.btnGo);
this.MaximumSize = new System.Drawing.Size(296, 300);
this.MinimumSize = new System.Drawing.Size(296, 300);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
string FileSizeString(long fileSize)
{
const long OneMB=1024*1024;
const long OneGB=OneMB*1024;
string unit;
double dispSize;
if(fileSize<1024)
{
unit="bytes";
dispSize=fileSize;
}
else if(fileSize<OneMB)
{
unit="KB";
dispSize=fileSize/1024;
}
else if(fileSize<OneGB)
{
unit="MB";
dispSize=fileSize/OneMB;
}
else
{
unit="GB";
dispSize=fileSize/OneGB;
}
return string.Format(" ({0,4:F} {1})",dispSize,unit);
}
long AddDirectory(DirectoryInfo dir,TreeNode tNode)
{
FileSystemInfo[] fSIs=dir.GetFileSystemInfos();
FileSystemInfo fSI;
int i;
long directorySize=0;
for(i=0;i<fSIs.Length;i++)
{
fSI=fSIs[i];
if(fSI.GetType()==typeof(FileInfo))
{
FileInfo fInfo=(FileInfo)fSI;
string s=fInfo.Name+FileSizeString(fInfo.Length);
TreeNode newTreeNode = new TreeNode(s);
tNode.Nodes.Add(newTreeNode);
directorySize+=fInfo.Length;
}
else if(fSI.GetType()==typeof(DirectoryInfo))
{
DirectoryInfo dirInfo=(DirectoryInfo)fSI;
TreeNode dirTreeNode=new TreeNode(dirInfo.Name);
long dirSize=AddDirectory(dirInfo,dirTreeNode);
dirTreeNode.Text+=FileSizeString(dirSize);
tNode.Nodes.Add(dirTreeNode);
directorySize+=dirSize;
}
else
MessageBox.Show("Ooops");
}
return directorySize;
}
private void BuildTreeView(string dirName)
{
DirectoryInfo dirInfo=new DirectoryInfo(dirName);
TreeNode tNode=new TreeNode(dirName);
long totalSize=AddDirectory(dirInfo,tNode);
tNode.Text+=FileSizeString(totalSize);
treeView.Nodes.Clear();
treeView.Nodes.Add(tNode);
}
private void btnGo_Click(object sender, System.EventArgs e)
{
BuildTreeView(txtPath.Text);
}
}
}