473,379 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Better Way to Get Directory Size

I have the following function to get the directory sizse, i wondering if
there any better way to find directory size
public static double GetDirectorySize(string DirectoryPath)
{
double fDirSize = 0 ;

if(!System.IO.Directory.Exists(DirectoryPath))
return fDirSize;
try
{
System.IO.DirectoryInfo dirInfo = new
System.IO.DirectoryInfo(DirectoryPath);
System.IO.FileInfo[] oFiles = dirInfo.GetFiles();
if(oFiles.Length > 0 )
{
int nFileLen = oFiles.Length;
for (int i=0 ; i < nFileLen ; i++)
fDirSize += oFiles[i].Length ;
}

System.IO.DirectoryInfo[] oDirectories = dirInfo.GetDirectories();
if (oDirectories.Length > 0 )
{
int nDirLen = oDirectories.Length;
for (int i=0 ; i < nDirLen ; i++)
fDirSize += GetDirectorySize(oDirectories[i].FullName);
}
}
catch(System.Exception ex)
{
throw new System.IO.IOException("GetDirectorySize >>" + ex.Message + ">>" +
ex.StackTrace);
}

return fDirSize ;
}
Nov 19 '05 #1
2 4015
Other than the fact that you can write the code more compactly, that's the
only way:

public static long Size(System.IO.DirectoryInfo dirInfo)
{
long total = 0;
foreach(System.IO.FileInfo file in dirInfo.GetFiles())}
total += file.Length;
}

foreach(System.IO.DirectoryInfo dir in dirInfo.GetDirectories()){
total += Size(dir);
}
return total;
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:10**********************************@microsof t.com...
I have the following function to get the directory sizse, i wondering if
there any better way to find directory size
public static double GetDirectorySize(string DirectoryPath)
{
double fDirSize = 0 ;

if(!System.IO.Directory.Exists(DirectoryPath))
return fDirSize;
try
{
System.IO.DirectoryInfo dirInfo = new
System.IO.DirectoryInfo(DirectoryPath);
System.IO.FileInfo[] oFiles = dirInfo.GetFiles();
if(oFiles.Length > 0 )
{
int nFileLen = oFiles.Length;
for (int i=0 ; i < nFileLen ; i++)
fDirSize += oFiles[i].Length ;
}

System.IO.DirectoryInfo[] oDirectories = dirInfo.GetDirectories();
if (oDirectories.Length > 0 )
{
int nDirLen = oDirectories.Length;
for (int i=0 ; i < nDirLen ; i++)
fDirSize += GetDirectorySize(oDirectories[i].FullName);
}
}
catch(System.Exception ex)
{
throw new System.IO.IOException("GetDirectorySize >>" + ex.Message + ">>"
+
ex.StackTrace);
}

return fDirSize ;
}

Nov 19 '05 #2
Oopss..forgot that you need that to be recursive :)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:10**********************************@microsof t.com...
I have the following function to get the directory sizse, i wondering if
there any better way to find directory size
public static double GetDirectorySize(string DirectoryPath)
{
double fDirSize = 0 ;

if(!System.IO.Directory.Exists(DirectoryPath))
return fDirSize;
try
{
System.IO.DirectoryInfo dirInfo = new
System.IO.DirectoryInfo(DirectoryPath);
System.IO.FileInfo[] oFiles = dirInfo.GetFiles();
if(oFiles.Length > 0 )
{
int nFileLen = oFiles.Length;
for (int i=0 ; i < nFileLen ; i++)
fDirSize += oFiles[i].Length ;
}

System.IO.DirectoryInfo[] oDirectories = dirInfo.GetDirectories();
if (oDirectories.Length > 0 )
{
int nDirLen = oDirectories.Length;
for (int i=0 ; i < nDirLen ; i++)
fDirSize += GetDirectorySize(oDirectories[i].FullName);
}
}
catch(System.Exception ex)
{
throw new System.IO.IOException("GetDirectorySize >>" + ex.Message + ">>"
+
ex.StackTrace);
}

return fDirSize ;
}

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: francisl | last post by:
How can we get a full directory size (sum of all his data)? like when we type `du -sh mydir` Because os.path.getsize('mydir') only give the size of the directory physical representation on the...
17
by: Joe Laughlin | last post by:
I've not used C much before, so I don't know how robust or good this code is. I'd appreciate any feedback or criticisms anyone has! Thanks, Joe #include <stdio.h> #include <string.h>
11
by: F. Michael Miller | last post by:
I'd like to copy the listing of a directory (& sub directories) to a text file in vb.net. I'm looking for teh equivilant of the DOS command dir /s > TargetFile.txt. Thanks!
1
by: Microsoft | last post by:
Is there a way to to retrieve Directory Property information? I am trying to develop a way to monitor total Directory size say any directory over 200 meg. I have the logic to enumerate the...
2
by: Phil Galey | last post by:
Using the following, you can determine the size of a file: Dim fi As New IO.FileInfo(<Path to file>) MsgBox(fi.Length) .... but what about the size of a directory? The IO.DirectoryInfo object...
2
by: mail2ganeshguru | last post by:
Friends, i want to calculate the size of the folders. Please give me some suggestion in this. thanks Ganesh.G
0
by: bcanter | last post by:
I found a file on the web that will allow you to enumerate groups but it was an .hta and the top level admins won't allow this. I need to give managers access to the groups so that when a new user is...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
6
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.