minhnhatisme

Modules

May 11th, 2021 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Threading;
  8.  
  9. namespace App
  10. {
  11.     class Modules
  12.     {
  13.  
  14.         public Modules()
  15.         {
  16.             LoadJunkPath(Temp);
  17.             LoadJunkPath(Prefetch);
  18.             LoadJunkPath(Recent);
  19.         }
  20.  
  21.         private string Temp = Environment.ExpandEnvironmentVariables("%temp%");
  22.  
  23.         private string Prefetch = Environment.ExpandEnvironmentVariables("%systemroot%") + @"\prefetch";
  24.  
  25.         private string Recent = Environment.ExpandEnvironmentVariables("%userprofile%") + @"\AppData\Roaming\Microsoft\Windows\Recent";
  26.  
  27.         private List<string> junkFilePathList = new List<string>();
  28.  
  29.         private List<string> junkDirectoryPathList = new List<string>();
  30.  
  31.         private void LoadJunkPath(string junkFolderPath)
  32.         {
  33.             foreach (var filePath in Directory.GetFiles(junkFolderPath))
  34.             {
  35.                 junkFilePathList.Add(filePath);
  36.             }
  37.             foreach (var directoryPath in Directory.GetDirectories(junkFolderPath))
  38.             {
  39.                 junkDirectoryPathList.Add(directoryPath);
  40.             };
  41.         }
  42.  
  43.         private void MessageLog(string type, string message)
  44.         {
  45.             Console.ResetColor();
  46.             if(type == "SUCCESS")
  47.             {
  48.                 Console.ForegroundColor = ConsoleColor.Green;
  49.             }
  50.             else if (type == "ERROR")
  51.             {
  52.                 Console.ForegroundColor = ConsoleColor.Red;
  53.             }
  54.             else
  55.             {
  56.                 Console.ForegroundColor = ConsoleColor.White;
  57.             }
  58.             Console.WriteLine(message);
  59.             Console.WriteLine(Environment.NewLine);
  60.         }
  61.  
  62.         public void Delete()
  63.         {
  64.             foreach (string item in junkFilePathList)
  65.             {
  66.                 try
  67.                 {
  68.  
  69.                     File.Delete(item);
  70.                     MessageLog("SUCCESS", "Deleted: " + item);
  71.                 }
  72.                 catch (Exception ex) { MessageLog("ERROR", "Error: " + ex.Message); }
  73.             }
  74.             foreach (string item in junkDirectoryPathList)
  75.             {
  76.                 try
  77.                 {
  78.  
  79.                     Directory.Delete(item, true);
  80.                     MessageLog("SUCCESS", "Deleted: " + item);
  81.                 }
  82.                 catch (Exception ex) { MessageLog("ERROR", "Error: " + ex.Message); }
  83.             }
  84.             Thread.Sleep(5000);
  85.             Environment.Exit(0);
  86.         }
  87.     }
  88. }
  89.  
Add Comment
Please, Sign In to add comment