C# get multiple folders size under upper folder and order them by size descendingly.

class Dirs
    {
        public string DirName { get; set; }
        public long DirSize { get; set; }
    }

 static void OrderFiles(string dir)
        {            
            string[] dirs = Directory.GetDirectories(dir);
            List dirsList = new List();
            foreach (var di in dirs)
            {
                try
                {
                    long dirSize = GetDirSize(di);
                    dirsList.Add(new Dirs() { DirName = di, DirSize = dirSize });
                }
                catch
                {
                }
            }

            var orderedList = dirsList.OrderByDescending(x => x.DirSize).ToList();
            foreach (var orderdir in orderedList)
            {
                Console.WriteLine(orderdir.DirName + "," + orderdir.DirSize);
            }

        }

        static long GetDirSize(string dir)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(dir);
            var allFiles = dirInfo.GetFiles("*", SearchOption.AllDirectories).ToList();
            long allFilesSize = 0;
            foreach (var file in allFiles)
            {
                allFilesSize += file.Length;
            }            
            return allFilesSize;
        }       

 

你可能感兴趣的:(C# get multiple folders size under upper folder and order them by size descendingly.)