// analyze the file and sum the size of files that have a same extension, // i.e. sum .doc with .doc, .exe with .exe, and so on private void GetFileTypeSize(DirectoryInfo d) { try { FileInfo[] fileInfos = d.GetFiles(); //discover all of the extension and sum their lenghts foreach (FileInfo fileInfo in fileInfos) { try { string fileExt = "*" + fileInfo.Extension.ToLower(); // Console.WriteLine(fileExt); // Add the file size with the same extension if (!DescoverResult.ContainsKey(fileExt)) { DescoverResult.Add(fileExt, (long)0); } else { //sum file lengths for each extensions DescoverResult[fileExt] = (long)DescoverResult[fileExt] + fileInfo.Length; //Console.WriteLine(fileInfo.Length); } } catch (Exception) { } } // Do the same process to the subdirectories DirectoryInfo[] dirInfos = d.GetDirectories(); foreach (DirectoryInfo dirInfo in dirInfos) { OnProgress(this, dirInfo.FullName); GetFileTypeSize(dirInfo); } } // Catch any exception if a folder/file cannot be accessed // e.g. due to security restriction catch (Exception) { } }
public override void analyze(string pPath) { long dirSize = 0; DescoverResult.Clear(); try { // Loop to explore all the directories under pPath foreach (string dirName in Directory.GetDirectories(pPath)) { // If the size information is already on the cache just // retrieve it from the cache, do not access the file system if (folderSizeCache.ContainsKey(dirName)) { dirSize = (long)folderSizeCache[dirName]; } else { try { // Access the file system to get the directory size System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(dirName); dirSize = GetDirectorySize(dirInfo); // Add the size to the cache so the subsequent call // doesn't need file access anymore folderSizeCache.Add(dirName, dirSize); } // Catch any exception if a folder cannot be accessed // e.g. due to security restriction catch (Exception) { } } // Add the result to the hashtable. This is the information // that is going to be displayed DescoverResult.Add(System.IO.Path.GetFileName(dirName), dirSize); } const string CURRENT_FOLDER_KEY = "(Current Directory)"; // Get the current directory size if it is already in cache if (folderSizeCache.ContainsKey(CURRENT_FOLDER_KEY)) { dirSize = (long)folderSizeCache[CURRENT_FOLDER_KEY]; } else { // Sum the file size under the current directory dirSize = 0; foreach (string fileName in System.IO.Directory.GetFiles(pPath)) { try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName); dirSize += fileInfo.Length; } // Catch any exception if a file cannot be accessed // e.g. due to security restriction catch (Exception) { } } // Add the result again to the hashtable DescoverResult.Add(CURRENT_FOLDER_KEY, dirSize); } } // Catch any exception if a folder cannot be accessed // e.g. due to security restriction catch (Exception) { } // Notify all the subscribers that the exploration has finished OnFinish(this); }