PIRATED 20130914 1707

PIRATED 20130914 1707

NOTE: This content appears to have been plagiarized. Please leave a comment or email tnwiki at Microsoft (with a link to this article) if we are mistaken. The content was pulled from the following source:
The community rules state:
  • "Someone else has relevant content and you want to help them share it with the world. It's a nice thought, but do not copy other people's content to the Wiki, even if the owner said it was OK."



.NET 4.0 introduces 7 New methods to Enumerate Directory and Files in .NET 4.0. All these methods return Enumerable Collections (IEnumerable<T>), which perform better than arrays. We will be using the DirectoryInfo.EnumerateDirectories and DirectoryInfo.EnumerateFiles in this sample which returns an enumerable collection of Directory and File information respectively.

Here’s how to calculate the size of a folder or directory using .NET 4.0 and LINQ. The code also calculates the size of all sub-directories.

C#

using System;
using System.Linq;
using System.IO;

namespace ConsoleApplication3
{
 class Program  {
 static void Main(string[] args)
    {
 DirectoryInfo dInfo = new DirectoryInfo(@"C:/Articles");
 // set bool parameter to false if you // do not want to include subdirectories. long sizeOfDir = DirectorySize(dInfo, true);

 Console.WriteLine("Directory size in Bytes : " +
 "{0:N0} Bytes", sizeOfDir);
 Console.WriteLine("Directory size in KB : " +
 "{0:N2} KB", ((double)sizeOfDir) / 1024);
 Console.WriteLine("Directory size in MB : " + 
 "{0:N2} MB", ((double)sizeOfDir) / (1024 * 1024));

 Console.ReadLine();
    }

 static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
    {
 // Enumerate all the files long totalSize = dInfo.EnumerateFiles()
                    .Sum(file => file.Length);

 // If Subdirectories are to be included if (includeSubDir)
       {
 // Enumerate all sub-directories          totalSize += dInfo.EnumerateDirectories()
                   .Sum(dir => DirectorySize(dir, true));
       }
 return totalSize;
    }
  }
}

VB.NET 10.0 (converted using online tool)

Imports System
Imports System.Linq
Imports System.IO

Module Module1Sub Main()
 Dim dInfo As New DirectoryInfo("C:/Articles")
 ' set bool parameter to false if you ' do not want to include subdirectories. Dim sizeOfDir As Long = DirectorySize(dInfo, True)

 Console.WriteLine("Directory size in Bytes : " & _
 "{0:N0} Bytes", sizeOfDir)
 Console.WriteLine("Directory size in KB : " & _
 "{0:N2} KB", (CDbl(sizeOfDir)) / 1024)
 Console.WriteLine("Directory size in MB : " & _
 "{0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))

 Console.ReadLine()
End SubPrivate Function DirectorySize(ByVal dInfo As DirectoryInfo, _
 ByVal includeSubDir As Boolean) As Long ' Enumerate all the files Dim totalSize As Long = dInfo.EnumerateFiles() _
     .Sum(Function(file) file.Length)

 ' If Subdirectories are to be included If includeSubDir Then ' Enumerate all sub-directories     totalSize += dInfo.EnumerateDirectories() _
      .Sum(Function(dir) DirectorySize(dir, True))
 End If Return totalSize
End FunctionEnd Module


OUTPUT

I got the following output after running this code.

image

To confirm that the code worked as expected, I opened Windows Explorer > C Drive > Right clicked the folder > Properties. Here is the screenshot that matches the output we got from our code

image

Source

This topic was created from this blog by Suprotim Agarwal.
 

Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Carsten Siemens edited Revision 4. Comment: Pirated Content - see my comment

  • XAML guy edited Revision 3. Comment: added original source link

  • Ed Price - MSFT edited Revision 1. Comment: Title casing. Adding tags.

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • LINQ posts is always helpful, there are advantages, maybe in perform/speed or something around this code?

  • Ed Price - MSFT edited Revision 1. Comment: Title casing. Adding tags.

  • Yeah it quite a general comment, but that won't be a factor with high speed device today ppl. are using min. of 2 GB RAM, dual core processor is minimum now, this will not be a concern when we see on its benifit

  • XAML guy edited Revision 3. Comment: added original source link

  • Carsten Siemens edited Revision 4. Comment: Pirated Content - see my comment

  • NOTE: This article was reported as Pirated/Plagiarized Content (content you didn't write) and will be removed. Please do not steal content from others. If you feel we are mistaken, please leave a comment or email tnwiki at Microsoft with a link to this article and with clear and detailed reasons why you own the content or have explicit permission from the author.

    Content was taken from: "Calculate the Size of a Folder/Directory using .NET 4.0"

    Published by Suprotim Agarwal on July 29, 2010

    www.devcurry.com/.../calculate-size-of-folderdirectory-using.html

Page 1 of 1 (6 items)