Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the best way to calculate the size of a directory in VB .NET?

I need to calculate the directory size in VB .Net

I know the following 2 methods

Method 1: from MSDN http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

' The following example calculates the size of a directory ' and its subdirectories, if any, and displays the total size ' in bytes.

Imports System
Imports System.IO

Public Class ShowDirSize

Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long
    Dim Size As Long = 0
    ' Add file sizes.
    Dim fis As FileInfo() = d.GetFiles()
    Dim fi As FileInfo
    For Each fi In fis
        Size += fi.Length
    Next fi
    ' Add subdirectory sizes.
    Dim dis As DirectoryInfo() = d.GetDirectories()
    Dim di As DirectoryInfo
    For Each di In dis
        Size += DirSize(di)
    Next di
    Return Size
End Function 'DirSize

Public Shared Sub Main(ByVal args() As String)
    If args.Length <> 1 Then
        Console.WriteLine("You must provide a directory argument at the command line.")
    Else
        Dim d As New DirectoryInfo(args(0))
        Dim dsize As Long = DirSize(d)
        Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, dsize)
    End If
End Sub 'Main
End Class 'ShowDirSize

Method 2: from What's the best way to calculate the size of a directory in .NET?

Dim size As Int64 = (From strFile In My.Computer.FileSystem.GetFiles(strFolder, _
              FileIO.SearchOption.SearchAllSubDirectories) _
              Select New System.IO.FileInfo(strFile).Length).Sum()

Both these methods work fine. However they take lot of time to calculate the directory size if there are lot of sub-folders. e.g i have a directory with 150,000 sub-folders. The above methods took around 1 hr 30 mins to calculate the size of the directory. However, if I check the size from windows it takes less than a minute.

Please suggest better and faster ways to calculate the size of the directory.

like image 220
Virendra Avatar asked Jul 08 '10 23:07

Virendra


People also ask

What is the command to know the size of a directory?

You can display the size of directories by using the du command and its options. Additionally, you can find the amount of disk space taken up by user accounts on local UFS file systems by using the quot command. For more information about these commands, see du(1M)and quot(1M).

What is the easiest way to check the size of a folder?

Open a file explorer window and right-click on the 'Name' field at the top. You'll see some options – specifically, options, that let you pick what sort of info you want to see about your folders. Select Size and the property will appear on the far right of your window.

How do you check size of a directory or file on your system?

Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

How do I find the size of a directory in DOS?

You will want to use dir /a/s so that it includes every file, including system and hidden files. This will give you the total size you desire.


1 Answers

Though this answer is talking about Python, the concept applies here as well.

Windows Explorer uses system API calls FindFirstFile and FindNextFile recursively to pull file information, and then can access the file sizes very quickly through the data that's passed back via a struct, WIN32_FIND_DATA: http://msdn.microsoft.com/en-us/library/aa365740(v=VS.85).aspx.

My suggestion would be to implement these API calls using P/Invoke, and I believe you will experience significant performance gains.

like image 176
rakuo15 Avatar answered Nov 09 '22 07:11

rakuo15