Advertisement
elena1234

DirectoryTraversal- copy the result always on desktop

Jan 17th, 2021 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace DirectoryTraversal
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var fileInfo = new Dictionary<string, Dictionary<string, double>>();
  13.             DirectoryInfo directoryInfo = new DirectoryInfo(@"..\..\..\");
  14.             var files = directoryInfo.GetFiles();
  15.             foreach (var file in files)
  16.             {
  17.                 if (!fileInfo.ContainsKey(file.Extension))
  18.                 {
  19.                     fileInfo[file.Extension] = new Dictionary<string, double>();
  20.                 }
  21.  
  22.                 fileInfo[file.Extension].Add(file.Name, file.Length / 1024.00);
  23.             }
  24.  
  25.             using (var writerInReport = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),     "report.txt"),true))
  26.  
  27.             {
  28.                 foreach (var (extension, nameLength) in fileInfo.OrderByDescending(f => f.Value.Count()).ThenBy(x => x.Key))
  29.                 {
  30.                     writerInReport.WriteLine(extension);
  31.                     foreach (var (name, length) in nameLength.OrderBy(f => f.Value))
  32.                     {
  33.                         writerInReport.WriteLine($"--{name} - {length:F3}kb");
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement