Click here to Skip to main content
15,890,506 members
Articles / DevOps
Tip/Trick

C#.NET Application to Speed Up Computer

Rate me:
Please Sign up or sign in to vote.
2.88/5 (7 votes)
4 May 2016CPOL1 min read 20.7K   768   10   6
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.

C#
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.

C#
//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.

C#
//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:

C#
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionerror Pin
Member 1336915610-Feb-19 22:15
Member 1336915610-Feb-19 22:15 
QuestionMost operations have no benefit Pin
Kirk Wood7-May-16 8:27
Kirk Wood7-May-16 8:27 
QuestionDelete of Cookies should be optional Pin
djmarcus6-May-16 4:07
djmarcus6-May-16 4:07 
Questionisnt path variable being used too many times???? Pin
Member 114143685-May-16 18:04
Member 114143685-May-16 18:04 
GeneralMy vote of 1 Pin
Nguyen.H.H.Dang5-May-16 13:39
professionalNguyen.H.H.Dang5-May-16 13:39 
QuestionRead-only files and other such attributes PinPopular
dandy724-May-16 9:34
dandy724-May-16 9:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.