Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all,

how do you get the size of a folder in vb.net?

With vb6 you could use the FileSystemObject and...

    
   Set fso = New FileSystemObject
   Set fsoFolder = fso.GetFolder(sPath)
   lDirSize = Val(fsoFolder.Size) 

isn't there a better way nowadays?

 

/Kejpa

Posted

kejpa

 

This is from .NET documentation (for C#). Hope it helps a bit.

 

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

using System;
using System.IO;

public class ShowDirSize 
{
   public static long DirSize(DirectoryInfo d) 
   {    
       long Size = 0;    
       // Add file sizes.
       FileInfo[] fis = d.GetFiles();
       foreach (FileInfo fi in fis) 
       {      
           Size += fi.Length;    
       }
       // Add subdirectory sizes.
       DirectoryInfo[] dis = d.GetDirectories();
       foreach (DirectoryInfo di in dis) 
       {
           Size += DirSize(di);   
       }
       return(Size);  
   }
   public static void Main(string[] args) 
   {
       if (args.Length != 1) 
       {
           Console.WriteLine("You must provide a directory argument at the command line.");    
       } 
       else 
       {  
           DirectoryInfo d = new DirectoryInfo(args[0]);
           Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d));
       }
   }
}

 

BOZ

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...