Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C#.NET Application to Speed Up Computer

0.00/5 (No votes)
4 May 2016 1  
Speed up your computer by deleting temporary data

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.

//Delete Internet Cache
var path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
di = new DirectoryInfo(path);
Console.WriteLine("****Deleting InternetCache progress****");
ClearTempData(di);
Console.WriteLine("****Finished Deleting InternetCache****");

//Delete Cookies
var cookie = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
di = new DirectoryInfo(path);
Console.WriteLine("****Deleting Cookies progress****");
ClearTempData(di);
Console.WriteLine("****Finished Deleting Cookies****");

//Delete History
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.

//Delete temporary folder
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:

//Delete RecycleBin
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
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here