Advertisement
Guest User

Directory Traversal

a guest
Dec 23rd, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         string directory = Console.ReadLine();
  11.         SortedDictionary<string, Dictionary<string, double>> extensions = new SortedDictionary<string, Dictionary<string, double>>();
  12.         DirectoryInfo directorySelected = new DirectoryInfo(directory);
  13.         FileInfo[] whole = directorySelected.GetFiles();
  14.  
  15.         GetFileExtensions(extensions, whole);
  16.  
  17.         GroupAndWrite(extensions);
  18.  
  19.     }
  20.     static void GroupAndWrite(SortedDictionary<string, Dictionary<string, double>> extensions)
  21.     {
  22.         var orderedExtension = extensions.OrderByDescending(p => p.Value.Count).ThenBy(ext => ext.Key);
  23.         using (StreamWriter destination = new StreamWriter(@"C:\Users\asus\Desktop\text.txt"))
  24.         {
  25.             foreach (var item in orderedExtension)
  26.             {
  27.                 destination.WriteLine(item.Key);
  28.                 var orderedDic = item.Value.OrderBy(f => f.Value);
  29.                 foreach (var output in orderedDic)
  30.                 {
  31.                     destination.WriteLine("{0}{1}kb", output.Key, output.Value / 1024);
  32.                 }
  33.             }
  34.         }
  35.     }
  36.     static void GetFileExtensions(SortedDictionary<string, Dictionary<string, double>> extensions, FileInfo[] files)
  37.     {  
  38.         foreach (var n in files)
  39.         {
  40.             if (!extensions.ContainsKey(n.Extension))
  41.             {
  42.                 extensions.Add(n.Extension, new Dictionary<string, double>
  43.                                     {{string.Format("--{0} - ", n.Name), n.Length}});
  44.             }
  45.             else
  46.             {
  47.                 extensions[n.Extension].Add(string.Format("--{0} - ", n.Name), n.Length);
  48.             }
  49.         }
  50.     }          
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement