Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get total size of particular folder in C#?

Tags:

asp.net

c#-4.0

I'm creating one application where I'm giving certain memory space to users and i want calculate the total space he used in his folder and to show him/her the total space utilized and total remaining space that can be utilized . How can I calculate size of entire folder including all files of particular folder in c#.

like image 880
user3248439 Avatar asked Jan 29 '14 10:01

user3248439


3 Answers

You can use the following function to calculate the size of specific folder.

Source: https://askgif.com/blog/144/how-can-i-get-the-total-size-of-a-particular-folder-in-c/ (Courtesy: https://askgif.com/)

static String GetDriveSize(String ParticularFolder, String drive)
    {
        String size = "";
        long MaxSpace = 10485760;
        String rootfoldersize = @"~/userspace/" + ParticularFolder+ "/";
        long totalbytes = 0;
        long percentageusage = 0;

        totalbytes = GetFolderSize(System.Web.HttpContext.Current.Server.MapPath(rootfoldersize) + "" + drive + "/");
        percentageusage = (totalbytes * 100) / MaxSpace;
        size = BytesToString(totalbytes);

        return size;
    }

static long GetFolderSize(string s)
    {
        string[] fileNames = Directory.GetFiles(s, "*.*");
        long size = 0;

        // Calculate total size by looping through files in the folder and totalling their sizes
        foreach (string name in fileNames)
        {
            // length of each file.
            FileInfo details = new FileInfo(name);
            size += details.Length;
        }
        return size;
    }

static String BytesToString(long byteCount)
    {
        string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
        if (byteCount == 0)
            return "0" + suf[0];
        long bytes = Math.Abs(byteCount);
        int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
        double num = Math.Round(bytes / Math.Pow(1024, place), 1);
        return (Math.Sign(byteCount) * num).ToString() + suf[place];
    }

Hope this will help you.

like image 59
Sumit Chourasia Avatar answered Oct 30 '22 00:10

Sumit Chourasia


You can use DirectoryInfo.GetFiles and FileInfo.Length:

DirectoryInfo dir = new DirectoryInfo(@"D:\Data\User_ABC");
FileInfo[] files = dir.GetFiles();
long totalByteSize = files.Sum(f => f.Length);

If you also want to include sub-sirectories:

FileInfo[] files = dir.GetFiles("*.*", SearchOption.AllDirectories);
like image 40
Tim Schmelter Avatar answered Oct 29 '22 23:10

Tim Schmelter


using System;
using System.IO;

class Program
{
    static void Main()
    {
        Console.WriteLine(GetDirectorySize("C:\\Site\\"));
    }

    static long GetDirectorySize(string p)
    {
        // 1.
        // Get array of all file names.
        string[] a = Directory.GetFiles(p, "*.*");

        // 2.
        // Calculate total bytes of all files in a loop.
        long b = 0;
        foreach (string name in a)
        {
            // 3.
            // Use FileInfo to get length of each file.
            FileInfo info = new FileInfo(name);
            b += info.Length;
        }
        // 4.
        // Return total size
        return b;
        }
}
like image 41
Furquan Khan Avatar answered Oct 29 '22 23:10

Furquan Khan