Author Topic: Code: Show memory for all folders on c drive  (Read 4026 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Code: Show memory for all folders on c drive
« on: December 28, 2006, 12:36:34 PM »
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: [Select]
/*
 * 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
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Code: Show memory for all folders on c drive
« Reply #1 on: January 01, 2007, 09:18:47 AM »
Nice one Tim - that's a usefull idea put into practice.

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Code: Show memory for all folders on c drive
« Reply #2 on: January 02, 2007, 11:28:41 AM »
Nice one Tim - that's a usefull idea put into practice.

Cheers,
Glenn.
Thanks Glenn.  :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Code: Show memory for all folders on c drive
« Reply #3 on: January 02, 2007, 11:42:19 AM »
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

  • Needs a day job
  • Posts: 5251
Re: Code: Show memory for all folders on c drive
« Reply #4 on: January 02, 2007, 11:48:33 AM »
That is a nice piece of code. .....  I like the color coding idea.
Thanks.

What would it take to add functionality to open the directory right from your window?
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?

I use Sequoia View to figure out what's taking up all the room on my HD
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.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Code: Show memory for all folders on c drive
« Reply #5 on: January 02, 2007, 12:56:46 PM »
Tim, I was thinking of a list of files, that way you could delete the offending files. The software I link to opens the folder of the file you're interested so you can delete or backup/remove if you like.

That sequoia view is an example of an interface that's different, but very functional.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Code: Show memory for all folders on c drive
« Reply #6 on: January 02, 2007, 01:04:31 PM »
Tim, I was thinking of a list of files, that way you could delete the offending files. The software I link to opens the folder of the file you're interested so you can delete or backup/remove if you like.

That sequoia view is an example of an interface that's different, but very functional.
I think this wouldn't be too hard, but that might be a false statement.  The list is easy enough to do (I think, as I have an idea how to do it), but being able to do stuff, that might take a little reading, but should be fun.  Will see what my work load today lets me play with.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Code: Show memory for all folders on c drive
« Reply #7 on: January 02, 2007, 06:44:59 PM »
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.
Code: [Select]
/*
 * 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();
}

}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.