Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. class DirectoryTraversal
  6. {
  7.     static void Main()
  8.     {
  9.         string[] files = Directory.GetFiles(Directory.GetCurrentDirectory());
  10.         var dir = new Dictionary<string, Dictionary<string, double>>();
  11.         // collect file info
  12.         foreach (var file in files)
  13.         {
  14.             var info = new FileInfo(file);
  15.             string extension = info.Extension;
  16.             string name = info.Name;
  17.             double size = info.Length;
  18.             if (!dir.Keys.Contains(extension))
  19.             {
  20.                 dir.Add(extension, new Dictionary<string, double>());
  21.             }
  22.             dir[extension].Add(name, size);
  23.         }
  24.         // sorting
  25.         var sortedOutput = dir
  26.             .OrderByDescending(filesCount => filesCount.Value.Keys.Count)
  27.             .ThenBy(extension => extension.Key)
  28.             .ThenBy(size => size.Value.Values); // sorting by filesize does not work properly
  29.         // write to file
  30.         string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  31.         using (StreamWriter writer = new StreamWriter(desktop + @"\" + "report.txt"))
  32.         {
  33.             foreach (var ext in sortedOutput)
  34.             {
  35.                 writer.WriteLine(ext.Key);
  36.                 foreach (var file in ext.Value)
  37.                 {
  38.                     writer.WriteLine("--{0} - {1:F3}kb", file.Key, file.Value / 1024);
  39.                 }
  40.             }
  41.         }
  42.         Console.WriteLine("Done, please check your Desktop folder.");
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement