How do i find the size of a folder in .net using any of the frameworks

How do i easy find the size of a folder in .net using any of the frameworks
paulCardiffAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DhaestConnect With a Mentor Commented:
Converted code for the second link
using Microsoft; 
using Microsoft.Win32; 
using Microsoft.Win32.Registry; 
using System.Collections; 
using System.Windows.Forms; 
using System.IO; 
using System.Threading; 
 
 
public bool abort; 
 
// Returns the sum of the files in the folder. 
// dPath: Path of the directory 
// include subfolders: set if include subfolders ;) 
public long GetFolderSize(string DirPath, bool includeSubFolders) 
{ 
    try { 
        long size = 0; 
        DirectoryInfo diBase = new DirectoryInfo(DirPath); 
        FileInfo[] files; 
        if (includeSubFolders) { 
            files = diBase.GetFiles("*", SearchOption.AllDirectories); 
        } 
        
        else { 
            files = diBase.GetFiles("*", SearchOption.TopDirectoryOnly); 
        } 
        IEnumerator ie = files.GetEnumerator; 
        while (ie.MoveNext & !abort) { 
            
            size += ((FileInfo)ie.Current).Length; 
        } 
        return size; 
    } 
    catch (Exception ex) { 
        Interaction.MsgBox("Error: " + ex.Message); 
        return -1; 
    } 
} 

Open in new window

0
 
DhaestCommented:
Converted code for the first link
using System.IO; 
public class FolderSize : System.Windows.Forms.Form 
{ 
    #region " Windows Form Designer generated code " 
    public FolderSize() : base() 
    { 
        //This call is required by the Windows Form Designer. 
        InitializeComponent(); 
        //Add any initialization after the InitializeComponent() call 
    } 
    //Form overrides dispose to clean up the component list. 
    protected override void Dispose(bool disposing) 
    { 
        if (disposing) { 
            if ((components != null)) { 
                components.Dispose(); 
            } 
        } 
        base.Dispose(disposing); 
    } 
    //Required by the Windows Form Designer 
    private System.ComponentModel.IContainer components; 
    //NOTE: The following procedure is required by the Windows Form Designer 
    //It can be modified using the Windows Form Designer. 
    //Do not modify it using the code editor. 
    internal System.Windows.Forms.Label Label1; 
    internal System.Windows.Forms.Button btnCalculate; 
    internal System.Windows.Forms.Button btnBrowse; 
    internal System.Windows.Forms.FolderBrowserDialog FolderBrowserDialog1; 
    internal System.Windows.Forms.TextBox txtLocation; 
    internal System.Windows.Forms.TextBox txtOutput; 
    internal System.Windows.Forms.Button Button1; 
    [System.Diagnostics.DebuggerStepThrough()] 
    private void InitializeComponent() 
    { 
        this.txtLocation = new System.Windows.Forms.TextBox(); 
        this.Label1 = new System.Windows.Forms.Label(); 
        this.txtOutput = new System.Windows.Forms.TextBox(); 
        this.btnCalculate = new System.Windows.Forms.Button(); 
        this.btnBrowse = new System.Windows.Forms.Button(); 
        this.FolderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); 
        this.Button1 = new System.Windows.Forms.Button(); 
        this.SuspendLayout(); 
        // 
        //txtLocation 
        // 
        this.txtLocation.Location = new System.Drawing.Point(80, 8); 
        this.txtLocation.Name = "txtLocation"; 
        this.txtLocation.Size = new System.Drawing.Size(336, 20); 
        this.txtLocation.TabIndex = 1; 
        this.txtLocation.Text = "D:\\CaseFiles"; 
        // 
        //Label1 
        // 
        this.Label1.Location = new System.Drawing.Point(20, 12); 
        this.Label1.Name = "Label1"; 
        this.Label1.Size = new System.Drawing.Size(60, 16); 
        this.Label1.TabIndex = 0; 
        this.Label1.Text = "Location >"; 
        // 
        //txtOutput 
        // 
        this.txtOutput.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.txtOutput.Font = new System.Drawing.Font("Lucida Console", 9.75f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (byte)0); 
        this.txtOutput.Location = new System.Drawing.Point(0, 34); 
        this.txtOutput.Multiline = true; 
        this.txtOutput.Name = "txtOutput"; 
        this.txtOutput.ScrollBars = System.Windows.Forms.ScrollBars.Both; 
        this.txtOutput.Size = new System.Drawing.Size(780, 491); 
        this.txtOutput.TabIndex = 5; 
        this.txtOutput.Text = ""; 
        // 
        //btnCalculate 
        // 
        this.btnCalculate.Location = new System.Drawing.Point(450, 8); 
        this.btnCalculate.Name = "btnCalculate"; 
        this.btnCalculate.Size = new System.Drawing.Size(92, 20); 
        this.btnCalculate.TabIndex = 3; 
        this.btnCalculate.Text = "&Calculate"; 
        // 
        //btnBrowse 
        // 
        this.btnBrowse.Location = new System.Drawing.Point(419, 8); 
        this.btnBrowse.Name = "btnBrowse"; 
        this.btnBrowse.Size = new System.Drawing.Size(27, 20); 
        this.btnBrowse.TabIndex = 2; 
        this.btnBrowse.Text = "..."; 
        // 
        //Button1 
        // 
        this.Button1.Location = new System.Drawing.Point(546, 8); 
        this.Button1.Name = "Button1"; 
        this.Button1.Size = new System.Drawing.Size(92, 20); 
        this.Button1.TabIndex = 4; 
        this.Button1.Text = "C&opy"; 
        // 
        //FolderSize 
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
        this.ClientSize = new System.Drawing.Size(780, 525); 
        this.Controls.Add(this.Button1); 
        this.Controls.Add(this.btnBrowse); 
        this.Controls.Add(this.btnCalculate); 
        this.Controls.Add(this.txtOutput); 
        this.Controls.Add(this.Label1); 
        this.Controls.Add(this.txtLocation); 
        this.Name = "FolderSize"; 
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
        this.Text = "Folder Size"; 
        this.ResumeLayout(false); 
    } 
    #endregion 
    private void btnBrowse_Click(object sender, System.EventArgs e) 
    { 
        FolderBrowserDialog1.ShowDialog(); 
        txtLocation.Text = FolderBrowserDialog1.SelectedPath(); 
    } 
    private void btnCalculate_Click(object sender, System.EventArgs e) 
    { 
        DirectoryInfo objDir = new DirectoryInfo(txtLocation.Text); 
        DirectoryInfo objFolder; 
        double dblTotalSize; 
        SortedList objArray = new SortedList(); 
        //I am using the SortedList since I don't want to do the Sort operation myself. 
        int i; 
        string strName; 
        double dblSize; 
        string strToList; 
        string strCCList = "dsinettl"; 
        try { 
            txtOutput.Text = string.Empty; 
            i = 0; 
            objArray.Clear(); 
            this.Text = "Please wait..."; 
            //Reading each Folder's name and Size 
            foreach ( objFolder in objDir.GetDirectories()) { 
                dblTotalSize = GetFolderSize(objFolder.FullName, true) / (1024 * 1024); 
                if (!(dblTotalSize == 0)) { 
                    objArray.Add(dblTotalSize, dblTotalSize.ToString + "\\" + objFolder.Name); 
                } 
                if (dblTotalSize > 5000) { 
                    strToList += objFolder.Name + ";"; 
                } 
                Application.DoEvents(); 
            } 
            txtOutput.Text += strToList + Constants.vbCrLf + Constants.vbCrLf + strCCList + Constants.vbCrLf + Constants.vbCrLf; 
            //Creating the output now in reverse order since I want it to be sorted by Descending Order 
            for (i = objArray.Count - 1; i >= 0; i += -1) { 
                strName = objArray.GetByIndex(i).ToString; 
                dblSize = strName.Substring(0, strName.IndexOf("\\")); 
                strName = strName.Substring(strName.IndexOf("\\") + 1); 
                txtOutput.Text += strName + Strings.Space(50 - strName.Length) + string.Format("{0, 10}", Strings.FormatNumber(dblSize, 2)) + " MB" + Constants.vbCrLf; 
            } 
            this.Text = "Folder Size - Done!"; 
        } 
        catch (Exception Ex) { 
            Interaction.MsgBox(Ex.Message); 
        } 
    } 
    //This is a recursive function which calculates the Folder Size 
    public long GetFolderSize(string DirPath, // ERROR: Unsupported modifier : In, Optional bool IncludeSubFolders) 
    { 
        long lngFolderSize; 
        FileInfo objFileInfo; 
        DirectoryInfo objFolder; 
        DirectoryInfo objSubFolder; 
        try { 
            objFolder = new DirectoryInfo(DirPath); 
            foreach ( objFileInfo in objFolder.GetFiles()) { 
                lngFolderSize += objFileInfo.Length; 
            } 
            if (IncludeSubFolders) { 
                foreach ( objSubFolder in objFolder.GetDirectories()) { 
                    lngFolderSize += GetFolderSize(objSubFolder.FullName); 
                } 
            } 
        } 
        catch (Exception Ex) { 
            Interaction.MsgBox(Ex.Message); 
        } 
        return lngFolderSize; 
    } 
    //Setting the data in txtOutput to the Clipboard 
    private void Button1_Click(object sender, System.EventArgs e) 
    { 
        Clipboard.SetDataObject(txtOutput.Text.ToString); 
    } 
} 

Open in new window

0
All Courses

From novice to tech pro — start learning today.