So it turned out a little harder that I thought it would be, but here is one that will list the files in the directory selected. I will improve it as I can. Right now you can't delete anything just see them. I also added an 'expand all' and 'collapse all' button. Have fun.
/*
* Created by SharpDevelop.
* Tim Willey
* Date: 12/27/2006
*
*/
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace MemoryManagmentDialog
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button CollapseAll;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button ExpandAll;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button ListFilesButton;
private System.Windows.Forms.Splitter splitter1;
public static double GetDirectorySize (DirectoryInfo Di, TreeNode ParentNode, bool IsFirst) {
double DirSize = 0;
try {
FileInfo[] fiAr = Di.GetFiles ();
foreach (FileInfo fi in fiAr) {
DirSize = DirSize + fi.Length;
}
DirectoryInfo[] diAr = Di.GetDirectories();
if (IsFirst == true) {
foreach (DirectoryInfo di in diAr) {
DirSize = DirSize + GetDirectorySize (di, ParentNode, false);
}
}
else {
TreeNode SubNode = new TreeNode (Di.Name);
foreach (DirectoryInfo di in diAr) {
DirSize = DirSize + GetDirectorySize (di, SubNode, false);
}
ParentNode.Nodes.Add (SubNode);
SubNode.Text = Di.Name + " - [ " + SizeToString (DirSize, SubNode) + " ]";
}
}
catch {
TreeNode SubNode = new TreeNode (Di.Name);
ParentNode.Nodes.Add (SubNode);
SubNode.Text = Di.Name + " ** [ Error ]";
SubNode.BackColor = Color.Red;
SubNode.ForeColor = Color.White;
}
return DirSize;
}
public static string SizeToString (Double dbl, TreeNode tn) {
string Str;
if (0 <= dbl && dbl < 1000) {
Str = dbl.ToString() + " b";
tn.BackColor = Color.Orange;
return Str;
}
else if (1000 < dbl && dbl < 1000000) {
Str = (dbl / 1000).ToString() + " KB";
tn.BackColor = Color.PaleGreen;
return Str;
}
else if (1000000 < dbl && dbl < 1000000000) {
Str = (dbl / 1000000).ToString() + " MB";
tn.BackColor = Color.DeepSkyBlue;
return Str;
}
else {
Str = (dbl / 1000000000).ToString() + " GB";
tn.BackColor = Color.White;
return Str;
}
}
void MainFormLoad (object sender, System.EventArgs e) {
DirectoryInfo di = new DirectoryInfo (@"c:\");
TreeNode MainNode = new TreeNode (di.Name);
treeView1.Nodes.Add (MainNode);
double MainSize = GetDirectorySize (di, MainNode, true);
MainNode.Text = di.Name + " - [ " + SizeToString (MainSize, MainNode) + " ]";
}
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, System.EventArgs e)
{
Close();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.splitter1 = new System.Windows.Forms.Splitter();
this.ListFilesButton = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.ExpandAll = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.listView1 = new System.Windows.Forms.ListView();
this.button1 = new System.Windows.Forms.Button();
this.CollapseAll = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(200, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 284);
this.splitter1.TabIndex = 2;
this.splitter1.TabStop = false;
//
// ListFilesButton
//
this.ListFilesButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ListFilesButton.Location = new System.Drawing.Point(244, 288);
this.ListFilesButton.Name = "ListFilesButton";
this.ListFilesButton.Size = new System.Drawing.Size(120, 24);
this.ListFilesButton.TabIndex = 4;
this.ListFilesButton.Text = "List files in directory";
this.ListFilesButton.Click += new System.EventHandler(this.ListFilesButtonClick);
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
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(200, 284);
this.treeView1.TabIndex = 1;
//
// ExpandAll
//
this.ExpandAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ExpandAll.Location = new System.Drawing.Point(8, 288);
this.ExpandAll.Name = "ExpandAll";
this.ExpandAll.Size = new System.Drawing.Size(72, 24);
this.ExpandAll.TabIndex = 6;
this.ExpandAll.Text = "Expand All";
this.ExpandAll.Click += new System.EventHandler(this.ExpandAllButtonClick);
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.Controls.Add(this.listView1);
this.panel1.Controls.Add(this.splitter1);
this.panel1.Controls.Add(this.treeView1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(372, 284);
this.panel1.TabIndex = 5;
//
// listView1
//
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(203, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(169, 284);
this.listView1.TabIndex = 3;
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(500, 291);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Done";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// CollapseAll
//
this.CollapseAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.CollapseAll.Location = new System.Drawing.Point(96, 288);
this.CollapseAll.Name = "CollapseAll";
this.CollapseAll.Size = new System.Drawing.Size(72, 24);
this.CollapseAll.TabIndex = 7;
this.CollapseAll.Text = "Collapse All";
this.CollapseAll.Click += new System.EventHandler(this.CollapseAllButtonClick);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(372, 317);
this.Controls.Add(this.CollapseAll);
this.Controls.Add(this.ExpandAll);
this.Controls.Add(this.panel1);
this.Controls.Add(this.ListFilesButton);
this.Controls.Add(this.button1);
this.DockPadding.All = 1;
this.MinimumSize = new System.Drawing.Size(380, 340);
this.Name = "MainForm";
this.Text = "Folder Memory Sizes";
this.Load += new System.EventHandler(this.MainFormLoad);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
void ListFilesButtonClick(object sender, System.EventArgs e)
{
TreeNode tn = treeView1.SelectedNode;
if (tn != null) {
listView1.Clear ();
string tnText = tn.Text;
tnText = tnText.Substring (0, tnText.IndexOf (" - "));
tn = tn.Parent;
while (tn != null) {
tnText = tn.Text.Substring (0, tn.Text.IndexOf (" - ")) + "\\" + tnText;
tn = tn.Parent;
}
//DirectoryInfo di = new DirectoryInfo (@"c:\\" + tnText +"\\");
DirectoryInfo di = new DirectoryInfo (tnText +"\\");
FileInfo[] fiAr = di.GetFiles();
if (fiAr.Length > 0) {
ListViewItem[] lviAr = new ListViewItem[fiAr.Length];
for (int i = 0; i < fiAr.Length; i++) {
lviAr[i] = new ListViewItem (fiAr[i].Name);
}
listView1.View = View.List;
listView1.Items.AddRange (lviAr);
}
}
}
void ExpandAllButtonClick(object sender, System.EventArgs e)
{
treeView1.ExpandAll();
}
void CollapseAllButtonClick(object sender, System.EventArgs e)
{
treeView1.CollapseAll();
}
}
}