step1:
Here take the folder which u want to find the size, then pass that folder to the recursive function name FolderSize!.
Expand|Select|Wrap|Line Numbers
- DirectoryInfo dirInfo = new DirectoryInfo(strPath) ;
- decimal dSize = 0;
- //If the folder exists then only its size will be calculated if not its size will be
- //zero!
- if (dirInfo.Exists)
- dSize = FolderSize(dirInfo);
Expand|Select|Wrap|Line Numbers
- #region FolderSize (Recursive Function)
- /// <summary>
- /// This method is used to find the size of the folder specified.
- /// This method is called recursively to find the length of the folder.
- /// </summary>
- /// <param name="dirInfo"></param>
- /// <returns></returns>
- protected decimal FolderSize(DirectoryInfo dirInfo)
- {
- decimal dSize = 0;
- //Size of the files in that specific fodler.
- foreach (FileInfo fileInfo in dirInfo.GetFiles())
- {
- dSize += Math.Abs((decimal)(fileInfo.Length / 1024.0));
- }
- //Size of the files in the child folders
- foreach (DirectoryInfo childDir in dirInfo.GetDirectories())
- {
- dSize += FolderSize(childDir);
- }
- return dSize;
- }
- #endregion
Thanks & Regards
Bharath Reddy VasiReddy