Click here to Skip to main content
13,833,874 members
Click here to Skip to main content
Add your own
alternative version


102 bookmarked
Posted 29 Dec 2007
Licenced CPOL

Disk Analyzer

, 25 Jan 2008
Rate this:
Please Sign up or sign in to vote.
A disk analyzer using C#.


Disk Analyzer enables you to understand how much space the files and directories on your disk have taken up, and helps you find files and folders that you no longer use. The tool analyses your disk drives and collects statistics of directory sizes and files sizes by type, which you can view as overview charts and details tables.

I am using the Pie Chart control developed by Julijan Sribar, for displaying the results. Really worth having a look. It's one of the best implementations of Pie Chart controls.

Also, I am using the tree grid view, which allows to display files by extension, implemented by Mark Rideout. You can visit his blog here.


Also, this program allows you to set up filters for the directories and files to be analyzed. I should agree, this needs to be enhanced to allow more customizability.


I was looking for a simple disk analyzer utility to calculate the size of the directory and to do some clean up and to help me organize my files better. I thought it would be good to write one.


  • Display size by directory with DataGridView and Pie Chart
  • Report size by file extension, with DataGridView and Pie Chart
  • Filter options by size; include and exclude file extensions; directories with zero bytes
  • Export the results to Excel; this is saved in Excel XML format, using the CarlosAg ExcelXmlWriter library
  • Open a file / folder and its parent directory

Using the code

Calculating the size of the directory is done by using a recursive function to calculate the size of each sub-directory, using LINQ:

public long CalculateSize(DirectoryInfo directory)
    CurrentDirectory = directory.FullName;
    this.BeginInvoke(new UIDelegate(updateCurrentStatus));
    if (abortFlag) return 0;
    long Size = 0;
    // Add file sizes.
    IEnumerable iEnumSize = from fi in directory.GetFiles() select fi.Lenght;
    Size += iEnumSize.Sum();
    // Add subdirectory sizes.
    IEnumerable<long /> iEnumSize = from di in directory.GetDirectories() 
                                       select CalculateSize(di);
    Size += iEnumSize.Sum();
    return Size;

Points of interest

Exporting data from DataGridView to Excel:


Below is the section of code to export data from a DataGridView to Excel, using the CarlosAg ExcelXMLWriter library. You can find more details on the CarlosAg ExcelXMLWriter library here. This allows you to create Excel files without using COM objects.

string filename = "myExcel.xls";
                Workbook book = new Workbook();
                book.ExcelWorkbook.ActiveSheetIndex = 1;
book.Properties.Author = "Disk Analyzer";
                book.Properties.Title = "Disk Analyzer Report";
                book.Properties.Created = DateTime.Now;

WorksheetStyle style = book.Styles.Add("HeaderStyle");
style.Font.FontName = "Tahoma";
style.Font.Size = 11;
style.Font.Bold = true;
style.Alignment.Horizontal = StyleHorizontalAlignment.Center;
style.Font.Color = "White";
style.Interior.Color = "Blue";
style.Interior.Pattern = StyleInteriorPattern.DiagCross;

// Create the Default Style to use for everyone
style = book.Styles.Add("Default");
style.Font.FontName = "Tahoma";
style.Font.Size = 10;

// Add a Worksheet with some data
Worksheet sheet = book.Worksheets.Add("Size Analysis");
WorksheetRow row = sheet.Table.Rows.Add();
// we can optionally set some column settings
for (int i = 0; i < dataGridViewDirectory.Columns.Count; i++)
  sheet.Table.Columns.Add(new WorksheetColumn(150));
  row.Cells.Add(new WorksheetCell(dataGridViewDirectory.Columns[i].Name, 

// Skip one row, and add some text
row.Index = 2;
for (int i = 0; i < dataGridViewDirectory.Rows.Count; i++)
  row = sheet.Table.Rows.Add();
  for (int j = 0; j < dataGridViewDirectory.Columns.Count; j++)


Updated the article source with project and solution files.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vivek Krishnamurthy
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood Job Pin
Shailesh Pisat5-Jul-09 22:47
professionalShailesh Pisat5-Jul-09 22:47 
Questionstyle.Interior.Color property doesn't work Pin
cghersi10-Feb-09 0:35
membercghersi10-Feb-09 0:35 
QuestionControl Error by Compiling with VS 2008 Pin
masterblaster20041-Jan-09 11:25
membermasterblaster20041-Jan-09 11:25 
Questionwhat if I turned off Visual Styles? Pin
Member 391999324-Aug-08 14:59
memberMember 391999324-Aug-08 14:59 
AnswerRe: what if I turned off Visual Styles? Pin
Vivek Krishnamurthy26-Aug-08 1:11
memberVivek Krishnamurthy26-Aug-08 1:11 
QuestionDefrag? Pin
Amrykid9-Feb-08 18:24
memberAmrykid9-Feb-08 18:24 
QuestionError When trying to run program Pin
gscheek24-Jan-08 10:52
membergscheek24-Jan-08 10:52 
GeneralRe: Error When trying to run program Pin
Vivek Krishnamurthy25-Jan-08 13:51
memberVivek Krishnamurthy25-Jan-08 13:51 
GeneralSort by "Total Size" is incorrect; Spelling; Unhandled exception Pin
H. Gohel15-Jan-08 14:41
memberH. Gohel15-Jan-08 14:41 
GeneralRe: Sort by "Total Size" is incorrect; Spelling; Unhandled exception Pin
Vivek Krishnamurthy25-Jan-08 13:44
memberVivek Krishnamurthy25-Jan-08 13:44 
GeneralNice article but please attach full solution [modified] Pin
Yogesh Jagota13-Jan-08 1:07
memberYogesh Jagota13-Jan-08 1:07 
GeneralRe: Nice article but please attach full solution Pin
Vivek Krishnamurthy14-Jan-08 3:33
memberVivek Krishnamurthy14-Jan-08 3:33 
GeneralRe: Nice article but please attach full solution Pin
Yogesh Jagota15-Jan-08 11:23
memberYogesh Jagota15-Jan-08 11:23 
GeneralRe: Nice article but please attach full solution Pin
Vivek Krishnamurthy25-Jan-08 13:38
memberVivek Krishnamurthy25-Jan-08 13:38 
GeneralRe: Nice article but please attach full solution Pin
MathiasW24-Jan-08 14:49
memberMathiasW24-Jan-08 14:49 
GeneralRe: Nice article but please attach full solution Pin
Vivek Krishnamurthy25-Jan-08 13:32
memberVivek Krishnamurthy25-Jan-08 13:32 
GeneralNice work! Pin
Bert delaVega8-Jan-08 6:01
memberBert delaVega8-Jan-08 6:01 
GeneralYou have this listed as C# 1.0 etc Pin
robvon8-Jan-08 1:00
memberrobvon8-Jan-08 1:00 
GeneralRe: You have this listed as C# 1.0 etc Pin
Vivek Krishnamurthy8-Jan-08 2:11
memberVivek Krishnamurthy8-Jan-08 2:11 
GeneralWish I could use it Pin
sides_dale7-Jan-08 18:06
membersides_dale7-Jan-08 18:06 
GeneralRe: Wish I could use it Pin
Vivek Krishnamurthy8-Jan-08 2:14
memberVivek Krishnamurthy8-Jan-08 2:14 
GeneralException opening System.Core assembly Pin
Yvan Rodrigues4-Jan-08 6:59
professionalYvan Rodrigues4-Jan-08 6:59 
GeneralRe: Exception opening System.Core assembly Pin
Vivek Krishnamurthy7-Jan-08 14:37
memberVivek Krishnamurthy7-Jan-08 14:37 
GeneralNice work Pin
Mohamed Y. Elamrani30-Dec-07 9:23
memberMohamed Y. Elamrani30-Dec-07 9:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190114.1 | Last Updated 25 Jan 2008
Article Copyright 2007 by Vivek Krishnamurthy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid