Code Red > .NET

Code: Show memory for all folders on c drive

(1/2) > >>

T.Willey:
I don't know if anyone else has had to do this, but I had to clear some hard drive space the other day, and was trying to find which directories has the most memory occupied.  Window explorer doesn't show you how much memory unless you right click, go to properties, so this would take too much time, so I thought I would try and write one.  Here is my attempt.  It works, and is color coded by how much each folder takes.

Hope others might find it useful also, and as always, any comments/tips welcomed.


--- Code: ---/*
 * 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 button1;
private System.Windows.Forms.TreeView treeView1;

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.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.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.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(512, 280);
this.treeView1.TabIndex = 1;
//
// 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(420, 287);
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);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.button1);
this.DockPadding.All = 1;
this.MinimumSize = new System.Drawing.Size(250, 340);
this.Text = "Folder Memory Sizes";
this.Name = "MainForm";
this.Load += new System.EventHandler(this.MainFormLoad);
this.ResumeLayout(false);
}
#endregion
}
}

--- End code ---

Glenn R:
Nice one Tim - that's a usefull idea put into practice.

Cheers,
Glenn.

T.Willey:

--- Quote from: Glenn R on January 01, 2007, 09:18:47 AM ---Nice one Tim - that's a usefull idea put into practice.

Cheers,
Glenn.

--- End quote ---
Thanks Glenn.  :-)

Atook:
That is a nice piece of code. What would it take to add functionality to open the directory right from your window? I like the color coding idea.

I use Sequoia View to figure out what's taking up all the room on my HD

T.Willey:

--- Quote from: Atook on January 02, 2007, 11:42:19 AM ---That is a nice piece of code. .....  I like the color coding idea.

--- End quote ---
Thanks.


--- Quote from: Atook on January 02, 2007, 11:42:19 AM --- What would it take to add functionality to open the directory right from your window?

--- End quote ---
I don't know.  I guess I could play around somemore with it, and see what I could come up with.  Are you wanting just a list of all the files?  Or are you thinking along the lines of doing something to the files?


--- Quote from: Atook on January 02, 2007, 11:42:19 AM ---I use Sequoia View to figure out what's taking up all the room on my HD

--- End quote ---
Thanks for the link.  I did a search before I wrote this, and didn't find anything.  I will look at it when I have a minute.

Navigation

[0] Message Index

[#] Next page

Go to full version