Guest User

Clean Drive

a guest
May 8th, 2012
546
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.IO;
  6. using System.Windows.Forms;
  7. using System.Security.Principal;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace SystemMaintenance
  11. {
  12.    
  13.  
  14.     class Program
  15.     {
  16.         [DllImport("Shell32.dll")]
  17.  
  18.         static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.                 string folderPath = string.Empty;
  23.  
  24.                 //Delete temporary files
  25.                 folderPath = System.Environment.GetEnvironmentVariable("temp");
  26.                 deleteFilesInDirectory(folderPath);
  27.  
  28.                 //Delete temporary internet files
  29.                 folderPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
  30.                 deleteFilesInDirectory(folderPath);          
  31.  
  32.                 //Empty recycle bin'
  33.                 try
  34.                 {
  35.                     SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOSOUND | RecycleFlags.SHERB_NOCONFIRMATION);
  36.                     MessageBox.Show("Recycle Bin has been emptied.");
  37.                 }
  38.                 catch (Exception ex)
  39.                 {
  40.                 MessageBox.Show(ex.ToString());
  41.                 }
  42.                
  43.         }      
  44.  
  45.         enum RecycleFlags : int
  46.         {
  47.  
  48.             // No confirmation dialog when emptying the recycle bin
  49.  
  50.             SHERB_NOCONFIRMATION = 0x00000001,
  51.  
  52.             // No progress tracking window during the emptying of the recycle bin
  53.  
  54.             SHERB_NOPROGRESSUI = 0x00000001,
  55.  
  56.             // No sound whent the emptying of the recycle bin is complete
  57.  
  58.             SHERB_NOSOUND = 0x00000004
  59.  
  60.         }
  61.  
  62.         public static void deleteFilesInDirectory(string folderPath)
  63.         {
  64.            
  65.             try
  66.             {
  67.                 string[] fileList = Directory.GetFiles(folderPath);
  68.  
  69.                 foreach (string file in fileList) {
  70.                     try
  71.                     {
  72.                         string fullName = file;
  73.                         File.Delete(file);
  74.                     }
  75.                     catch (Exception ex)
  76.                     {
  77.                        
  78.                     }
  79.                 }
  80.                              
  81.                 MessageBox.Show(folderPath + " has been cleaned.");
  82.             }
  83.             catch (System.IO.IOException ex)
  84.             {
  85.                 MessageBox.Show(ex.Message);
  86.                 return;
  87.  
  88.             }
  89.         }        
  90.  
  91.  
  92.  
  93.  
  94.  
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment