Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- using System.Security.Principal;
- using System.Runtime.InteropServices;
- namespace SystemMaintenance
- {
- class Program
- {
- [DllImport("Shell32.dll")]
- static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);
- static void Main(string[] args)
- {
- string folderPath = string.Empty;
- //Delete temporary files
- folderPath = System.Environment.GetEnvironmentVariable("temp");
- deleteFilesInDirectory(folderPath);
- //Delete temporary internet files
- folderPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
- deleteFilesInDirectory(folderPath);
- //Empty recycle bin'
- try
- {
- SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOSOUND | RecycleFlags.SHERB_NOCONFIRMATION);
- MessageBox.Show("Recycle Bin has been emptied.");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- enum RecycleFlags : int
- {
- // No confirmation dialog when emptying the recycle bin
- SHERB_NOCONFIRMATION = 0x00000001,
- // No progress tracking window during the emptying of the recycle bin
- SHERB_NOPROGRESSUI = 0x00000001,
- // No sound whent the emptying of the recycle bin is complete
- SHERB_NOSOUND = 0x00000004
- }
- public static void deleteFilesInDirectory(string folderPath)
- {
- try
- {
- string[] fileList = Directory.GetFiles(folderPath);
- foreach (string file in fileList) {
- try
- {
- string fullName = file;
- File.Delete(file);
- }
- catch (Exception ex)
- {
- }
- }
- MessageBox.Show(folderPath + " has been cleaned.");
- }
- catch (System.IO.IOException ex)
- {
- MessageBox.Show(ex.Message);
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment