示例#1
0
        /// <summary>
        /// Gets disk usage information for a directory.
        /// </summary>
        /// <param name="directory">Specifies the directory where to look for objects</param>
        /// <param name="summary">Whether to summarize the result (show only direct children), or display all descendats.</param>
        /// <returns>An array of DiskUsageEntry objects.</returns>
        public static DiskUsageEntry[] GetDiskUsage(string directory, bool summary)
        {
            Dictionary <string, long> allObjects;

            if (summary)
            {
                allObjects = new Dictionary <string, long>();

                try
                {
                    string[] allFiles = Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly);
                    foreach (string file in allFiles)
                    {
                        allObjects.Add(file, GetFileSize(file));
                    }
                }
                catch (DirectoryNotFoundException)
                {
                }

                try
                {
                    string[] allDirectories = Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly);
                    foreach (string dir in allDirectories)
                    {
                        allObjects.Add(dir, GetDirectorySize(dir, true, null, null));
                    }
                }
                catch (DirectoryNotFoundException)
                {
                }
            }
            else
            {
                allObjects = new Dictionary <string, long>();
                GetDirectorySize(directory, true, allObjects, new object());
                allObjects.Remove(directory);
            }

            List <DiskUsageEntry> result = new List <DiskUsageEntry>(allObjects.Count);

            foreach (string obj in allObjects.Keys)
            {
                DiskUsageEntry entry = new DiskUsageEntry(GetReadableForm(allObjects[obj]), allObjects[obj], obj);
                result.Add(entry);
            }

            return((from du in result orderby du.Directory select du).ToArray());
        }
示例#2
0
        /// <summary>
        /// Gets disk usage information for a directory.
        /// </summary>
        /// <param name="directory">Specifies the directory where to look for objects</param>
        /// <param name="summary">Whether to summarize the result (show only direct children), or display all descendats.</param>
        /// <returns>An array of DiskUsageEntry objects.</returns>
        public static DiskUsageEntry[] GetDiskUsage(string directory, bool summary)
        {
            Dictionary<string, long> allObjects;

            if (summary)
            {
                allObjects = new Dictionary<string, long>();

                try
                {
                    string[] allFiles = Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly);
                    foreach (string file in allFiles)
                    {
                        allObjects.Add(file, GetFileSize(file));
                    }
                }
                catch (DirectoryNotFoundException)
                {
                }

                try
                {
                    string[] allDirectories = Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly);
                    foreach (string dir in allDirectories)
                    {
                        allObjects.Add(dir, GetDirectorySize(dir, true, null, null));
                    }
                }
                catch (DirectoryNotFoundException)
                {
                }
            }
            else
            {
                allObjects = new Dictionary<string, long>();
                GetDirectorySize(directory, true, allObjects, new object());
                allObjects.Remove(directory);
            }

            List<DiskUsageEntry> result = new List<DiskUsageEntry>(allObjects.Count);

            foreach (string obj in allObjects.Keys)
            {
                DiskUsageEntry entry = new DiskUsageEntry(GetReadableForm(allObjects[obj]), allObjects[obj], obj);
                result.Add(entry);
            }

            return (from du in result orderby du.Directory select du).ToArray();
        }