Folder size

  • Thread starter Thread starter Jesper, Denmark
  • Start date Start date
J

Jesper, Denmark

Hi,

Can anyone explain me the easiet way to get the size of a folder, including
all files subfolders and.

Something like DirectoryInfo.Size, helas, its not there.

regards Jesper.
 
Hi,

Can anyone explain me the easiet way to get the size of a folder, including
all  files subfolders and.

Something like DirectoryInfo.Size, helas, its not there.

regards Jesper.

You will have to add up the size of all files manually, e.g.

public static long GetDirectorySize(string path)
{
long size = 0L;
string[] files = Directory.GetFiles(path);
if(files != null)
{
for(int i = 0; i < files.Length; i++)
{
FileInfo fileInfo = new FileInfo(files);
size += fileInfo.Length;
}
}
string[] subDirs = Directory.GetDirectories(path);
if(subDirs != null)
{
for(int i = 0; i < subDirs.Length; i++)
{
size += GetDirectorySize(subDirs);
}
}
return size;
}
 
Can anyone explain me the easiet way to get the size of a folder, including
all files subfolders and.
Something like DirectoryInfo.Size, helas, its not there.
regards Jesper.

You will have to add up the size of all files manually, e.g.

public static long GetDirectorySize(string path)
{
long size = 0L;
string[] files = Directory.GetFiles(path);
if(files != null)
{
for(int i = 0; i < files.Length; i++)
{
FileInfo fileInfo = new FileInfo(files);
size += fileInfo.Length;
}
}
string[] subDirs = Directory.GetDirectories(path);
if(subDirs != null)
{
for(int i = 0; i < subDirs.Length; i++)
{
size += GetDirectorySize(subDirs);
}
}
return size;

}


=================================

Same Logic... Optimized.....

private long GetDirectorySize(DirectoryInfo di)
{
long size = 0L;

foreach (FileInfo fi in di.GetFiles())
{
size += fi.Length;
}

foreach (DirectoryInfo subdir in di.GetDirectories())
{
GetDirectorySize(subdir);
}

return size;
}

===============================

-Cnu
 
slight typo, inline

Duggi wrote on 20-6-2008 :
=================================

Same Logic... Optimized.....

private long GetDirectorySize(DirectoryInfo di)
{
long size = 0L;

foreach (FileInfo fi in di.GetFiles())
{
size += fi.Length;
}

foreach (DirectoryInfo subdir in di.GetDirectories())
{ size +=
GetDirectorySize(subdir);
}

return size;
}

===============================

-Cnu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top