Introduction
In this article, you will see an example that shows how to clear temporary data such as internet cache, browser cookies, history, temporary files, application data temporary files and empty recycle bin using C# console application. This will speed up your system to some extent. Yes, regular deleting temporary files does seed up your computer, but amount of speed up depends on memory, processor and how often this folder is used. Windows loves to write files all the time to your hard drive, and until you run out of room on your hard drive or ask it to clean it up, it will leave old temporary files there.
Background
Every other week, I end up deleting temporary files and folders, removing cache, history, etc. and spend around 5-10 minutes so I decided to write a C# console application that will delete temporary data and empty recycle bin within few seconds.
Using the Code
Create a new solution using any Visual Studio and add console project.
Console.WriteLine("1.Internet Cache");
Console.WriteLine("2.Cookies");
Console.WriteLine("3.History");
Console.WriteLine("4.Temp files");
Console.WriteLine("5.App Data Temp files");
Console.WriteLine("6.RecyleBin (Message prompts for asking delete all files!!!)");
Console.WriteLine("--------------------------------------------------");
Console.Write("Are you sure you want to Delete above items? Y/N: ");
var str = Console.ReadLine();
ReadLine
reads the next line of characters from the standard input stream. In this case, you have to enter Y
or y
.
Clearing the internet cache, cookies and history: Generally, you can delete using Internet Explorer, Tools menu => Internet option but using the below code, you can completely clear the cache, delete cookies and browsing history without opening browser.
var path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
di = new DirectoryInfo(path);
Console.WriteLine("****Deleting InternetCache progress****");
ClearTempData(di);
Console.WriteLine("****Finished Deleting InternetCache****");
var cookie = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
di = new DirectoryInfo(path);
Console.WriteLine("****Deleting Cookies progress****");
ClearTempData(di);
Console.WriteLine("****Finished Deleting Cookies****");
var history = Environment.GetFolderPath(Environment.SpecialFolder.History);
di = new DirectoryInfo(path);
Console.WriteLine("****Deleting History progress****");
ClearTempData(di);
Console.WriteLine("****Finished Deleting History****");
Deleting temporary folder and files: To keep your computer in good working order, it’s important to ensure that unused files are removed on a regular basis. The code below will delete temporary folder and files.
di = new DirectoryInfo(@"C:\Windows\Temp");
Console.WriteLine("****Deleting Temp files progress****");
ClearTempData(di);
Console.WriteLine("****Finished Deleting Temp files****");
di = new DirectoryInfo(System.IO.Path.GetTempPath());
Console.WriteLine("****Deleting App Data Temp files progress****");
ClearTempData(di);
Console.WriteLine("****Finished Deleting App Data Temp files****");
And finally, empty recycle bin:
Console.WriteLine("****Deleting RecyleBin****");
uint result = SHEmptyRecycleBin(IntPtr.Zero, null, 0);
Console.WriteLine("****Finished Deleting RecyleBin****");
Console.WriteLine("****************************************************************");
Console.WriteLine("Process Completed!!!....Press any key to exit!!!....");
Console.ReadLine();
private static void ClearTempData(DirectoryInfo di)
{
foreach (FileInfo file in di.GetFiles())
{
try
{
file.Delete();
Console.WriteLine(file.FullName);
}
catch (Exception ex)
{
continue;
}
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
try
{
dir.Delete(true);
Console.WriteLine(dir.FullName);
}
catch (Exception ex)
{
continue;
}
}
}
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
RecycleFlags dwFlags);
enum RecycleFlags : uint
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000001,
SHERB_NOSOUND = 0x00000004
}