Introduction
Temporary files: We all have them, and we all hate them, especially when apps are starting to perform poorly or space on the hard drives start to run out. With this article, I will show you how easy it is to create a Temp file cleaner.
Our Project
Open Visual Studio and create either a C# or VB.NET Windows Forms application. Design the form to look similar to Figure 1.
Figure 1: Design
Ensure you have the following namespaces included in your class.
C#
using Microsoft.Win32; using System.IO;
VB.NET
Imports System.IO Imports Microsoft.Win32
Declare the following modular variables:
C#
string strCommonApplicationData = Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData); string strLocalApplicationData = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData); string strWinDir = Environment.GetEnvironmentVariable ("WINDIR", EnvironmentVariableTarget.Machine); string strApplicationData = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); string strLowLocalApplicationData = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData) + "Low"; public string strVSFileMRUList = "SOFTWARE\\Microsoft\\VisualStudio\\17.0\\FileMRUList"; public string strVSProjectMRUList = "SOFTWARE\\Microsoft\\VisualStudio\\17.0\\ProjectMRUList";
VB.NET
Dim strCommonApplicationData As String = Environment _ .GetFolderPath(Environment.SpecialFolder _ .CommonApplicationData) Dim strLocalApplicationData As String = Environment _ .GetFolderPath(Environment.SpecialFolder _ .LocalApplicationData) Dim strWinDir As String = Environment.GetEnvironmentVariable _ ("WINDIR", EnvironmentVariableTarget.Machine) Dim strApplicationData As String = Environment.GetFolderPath _ (Environment.SpecialFolder.ApplicationData) Dim strLowLocalApplicationData As String = Environment _ .GetFolderPath(Environment _ .SpecialFolder.LocalApplicationData) & "Low"
The preceding few variables store the following locations into memory. That way, you won’t have to retype them every time you need them:
- C:\Users\myUsername\AppData\Roaming
- C:\ProgramData
- C:\Users\myUsername\AppData\Local
- C:\Users\myUsername\AppData\LocalLow
Add the following variables to store Registry key locations:
C#
public string strVSFileMRUList = "SOFTWARE\\Microsoft\\ VisualStudio\\17.0\\FileMRUList"; public string strVSProjectMRUList = "SOFTWARE\\Microsoft\\ VisualStudio\\17.0\\ProjectMRUList";
VB.NET
Public strVSFileMRUList As String = "SOFTWARE\Microsoft _ \VisualStudio\17.0\FileMRUList" Public strVSProjectMRUList As String = "SOFTWARE\Microsoft _ \VisualStudio\17.0\ProjectMRUList"
These Registry keys contain Visual Studio’s recent file lists and project lists.
Add the following code to delete Internet Explorer’s and Google Chrome’s temporary files:
C#
private void Button1_Click(object sender, EventArgs e) { List<string> lstIE = new List<string>(); string IEReg; lstIE.Add(strApplicationData + "\\Microsoft\\Windows\\ Cookies"); lstIE.Add(strLowLocalApplicationData + "\\Microsoft\\ Internet Explorer\\DOMStore"); lstIE.Add(strLocalApplicationData + "\\Microsoft\\ Windows\\Temporary Internet Files"); lstIE.Add(strLocalApplicationData + "\\Microsoft\\ Windows\\History"); lstIE.Add("SOFTWARE\\Microsoft\\Internet Explorer\\ TypedURLs"); IEReg = "SOFTWARE\\Microsoft\\Internet Explorer\\ TypedURLs"; foreach (var item in lstIE) { DeleteFilesRecursive(item); } Registry.LocalMachine.DeleteSubKey(IEReg); } private void Button2_Click(object sender, EventArgs e) { List<string> lstChrome = new List<string>(); lstChrome.Add(strLocalApplicationData + "\\Google\\ Chrome\\User Data\\Default\\Local Storage"); lstChrome.Add(strLocalApplicationData + "\\Google\\ Chrome\\User Data\\Default\\databases"); lstChrome.Add(strLocalApplicationData + "\\Google\\ Chrome\\User Data\\Default\\Cookies"); lstChrome.Add(strLocalApplicationData + "\\Google\\ Chrome\\User Data\\Default\\Cache"); lstChrome.Add(strLocalApplicationData + "\\Google\\ Chrome\\User Data\\Default"); foreach (var item in lstChrome) { DeleteFilesRecursive(item); } }
VB.NET
Private Sub Button1_Click(sender As Object, e As EventArgs) _ Handles Button1.Click Dim lstIE As List(Of String) = New List(Of String) Dim IEReg As String lstIE.Add(strApplicationData & "\Microsoft\Windows\ _ Cookies") lstIE.Add(strLowLocalApplicationData & "\Microsoft\ _ Internet Explorer\DOMStore") lstIE.Add(strLocalApplicationData & "\Microsoft\ _ Windows\Temporary Internet Files") lstIE.Add(strLocalApplicationData & "\Microsoft\ _ Windows\History") lstIE.Add("SOFTWARE\Microsoft\Internet Explorer\TypedURLs") IEReg = "SOFTWARE\Microsoft\Internet Explorer\TypedURLs" For Each item In lstIE DeleteFilesRecursive(item) Next My.Computer.Registry.LocalMachine.DeleteSubKey(IEReg) End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) _ Handles Button2.Click Dim lstChrome As List(Of String) = New List(Of String) lstChrome.Add(strLocalApplicationData & "\Google\Chrome _ \User Data\Default\Local Storage") lstChrome.Add(strLocalApplicationData & "\Google\Chrome _ \User Data\Default\databases") lstChrome.Add(strLocalApplicationData & "\Google\Chrome _ \User Data\Default\Cookies") lstChrome.Add(strLocalApplicationData & "\Google\Chrome _ \User Data\Default\Cache") lstChrome.Add(strLocalApplicationData & "\Google\Chrome _ \User Data\Default") For Each item In lstChrome DeleteFilesRecursive(item) Next End Sub
I created a list object and populated it with all the desired files and folders that contain temporary files for Internet Explorer and Chrome. I then called the DeleteFilesRecursive sub for every folder or file in the list. Let’s add the DeleteFilesRecursive sub now.
C#
private void DeleteFilesRecursive(string strFolder) { foreach (var dirFolder in Directory.GetDirectories (strFolder)) { Directory.Delete(dirFolder, true); } foreach (var fil in Directory.GetFiles(strFolder)) { File.Delete(fil); } }
VB.NET
Private Sub DeleteFilesRecursive(strFolder As String) For Each dirFolder In Directory.GetDirectories(strFolder) Directory.Delete(dirFolder, True) Next For Each file In Directory.GetFiles(strFolder) File.Delete(file) Next End Sub
A recursive sub or method is a sub or method that calls itself. Add the following code to delete Windows’ temporary files:
C#
private void Button3_Click(object sender, EventArgs e) { List<string> lstWinExplorer = new List <string>(); lstWinExplorer.Add(strApplicationData + "\\Microsoft\\ Windows\\Recent"); lstWinExplorer.Add(strLocalApplicationData + "\\MICROSOFT \\Windows\\Explorer"); foreach (var item in lstWinExplorer) { DeleteFilesRecursive(item); } } private void Button4_Click(object sender, EventArgs e) { List<string> lstWinSystem = new List<string>(); lstWinSystem.Add(strWinDir + "\\Temp"); lstWinSystem.Add(strLocalApplicationData + "\\Temp"); lstWinSystem.Add(strWinDir + "\\MiniDump"); lstWinSystem.Add(strWinDir); lstWinSystem.Add(strLocalApplicationData + "\\Microsoft\\ Windows\\WER\\ReportArchive"); lstWinSystem.Add(strLocalApplicationData + "\\Microsoft\\ Windows\\WER\\ReportQueue"); lstWinSystem.Add(strCommonApplicationData + "\\Microsoft\\ Windows\\WER\\ReportArchive"); lstWinSystem.Add(strCommonApplicationData + "\\Microsoft\\ Windows\\WER\\ReportQueue"); foreach (var item in lstWinSystem) { DeleteFilesRecursive(item); } }
VB.NET
Private Sub Button3_Click(sender As Object, e As EventArgs) _ Handles Button3.Click Dim lstWinExplorer As List(Of String) = New List(Of String) lstWinExplorer.Add(strApplicationData & _ "\Microsoft\Windows\Recent") lstWinExplorer.Add(strLocalApplicationData & _ "\MICROSOFT\Windows\Explorer") For Each item In lstWinExplorer DeleteFilesRecursive(item) Next End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) _ Handles Button4.Click Dim lstWinSystem As List(Of String) = New List(Of String) lstWinSystem.Add(strWinDir & "\Temp") lstWinSystem.Add(strLocalApplicationData & "\Temp") lstWinSystem.Add(strWinDir & "\MiniDump") lstWinSystem.Add(strWinDir) lstWinSystem.Add(strLocalApplicationData & _ "\Microsoft\Windows\WER\ReportArchive") lstWinSystem.Add(strLocalApplicationData & _ "\Microsoft\Windows\WER\ReportQueue") lstWinSystem.Add(strCommonApplicationData & _ "\Microsoft\Windows\WER\ReportArchive") lstWinSystem.Add(strCommonApplicationData & _ "\Microsoft\Windows\WER\ReportQueue") For Each item In lstWinSystem DeleteFilesRecursive(item) Next End Sub
Delete Windows Media Player’s temporary files:
C#
private void Button6_Click(object sender, EventArgs e) { List<string> lstMedia = new List<string>(); lstMedia.Add(strLocalApplicationData + "\\Microsoft\\Media Player"); foreach (var item in lstMedia) { DeleteFilesRecursive(item); } }
VB.NET
Private Sub Button6_Click(sender As Object, e As EventArgs) _ Handles Button6.Click Dim lstMedia As List(Of String) = New List(Of String) lstMedia.Add(strLocalApplicationData & _ "\Microsoft\Media Player") For Each item In lstMedia DeleteFilesRecursive(item) Next End Sub
Delete Visual Studio’s temporary settings:
C#
private void Button5_Click(object sender, EventArgs e) { List<string> lstVS = new List<string>(); lstVS.Add("SOFTWARE\\Microsoft\\VisualStudio\\17.0\\ FileMRUList"); lstVS.Add("SOFTWARE\\Microsoft\\VisualStudio\\17.0\\ ProjectMRUList"); foreach (var item in lstVS) { Registry.LocalMachine.DeleteSubKey(item); } }
VB.NET
Private Sub Button5_Click(sender As Object, e As EventArgs) _ Handles Button5.Click Dim lstVS As List(Of String) = New List(Of String) lstVS.Add("SOFTWARE\Microsoft\VisualStudio\17.0\FileMRUList") lstVS.Add("SOFTWARE\Microsoft\VisualStudio\17.0\ _ ProjectMRUList") For Each item In lstVS My.Computer.Registry.LocalMachine.DeleteSubKey(item) Next End Sub
Here, I looped through the specific Registry keys and deleted them.
Conclusion
This project can be built upon, depending on where the folder locations and Registry keys are for other applications not mentioned here. Creating an application that can clean up Temporary files is not difficult—you just need a bit of logic and the locations of each program’s files. I hope you have enjoyed today’s article.