I am not sure this is the best way to deal with these unwanted files. YES I wrote a small VB.Net application to delete chrome cache data files that have this format "f_00006a" & another file that shows up in a Temp folder every time I start my computer "dd_updateconfiuration_202.log" and if my computer is idle for 10 min or more Visual Studio tries to update and adds a bunch of these dd files to the temp folder REAL Annoying YES I have VS 2019 and have it set to not request updates Here is the code I wrote to deal with this feel free to offer suggestions for improvements to this code. It works fine but seems like overkill to deal with the situation.
Public Class frmStart
Public Sub frmStart_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If Asc(e.KeyChar) = 27 Then
Application.Exit()
End If
End Sub
Private Sub btnVCache_Click(sender As Object, e As EventArgs) Handles btnVCache.Click
lstFiles.Items.Clear()
Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data\"
Process.Start(folderPath)
'Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub btnVTemp_Click(sender As Object, e As EventArgs) Handles btnVTemp.Click
lstFiles.Items.Clear()
Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Temp\"
Process.Start(folderPath)
End Sub
Private Sub btnDelCache_Click(sender As Object, e As EventArgs) Handles btnDelCache.Click
Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data\"
Dim filePattern As String = "f*"
Dim result As MsgBoxResult = Nothing
Dim files As IEnumerable(Of String) = Directory.EnumerateFiles(folderPath, filePattern)
lstFiles.Items.Clear() 'WORKS where to use it and HOW TO USE
Try
If files.Any() Then
MsgBox("Files Found", vbOKOnly, "File Deletion")
result = MsgBox("Yes to Delete Files" & vbCrLf & "NO ONLY View Files", vbYesNo, "File Deletion")
If result = MsgBoxResult.Yes Then
For Each fileName As String In Directory.GetFiles(folderPath)
Dim dirInfo As New DirectoryInfo(fileName)
Dim fC As String = fileName.Replace(folderPath, String.Empty)
If fC.Substring(0, 1) = "f" Then
lstFiles.Items.Add(fileName.Replace(folderPath, String.Empty))
My.Computer.FileSystem.DeleteFile(fileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
End If
Next
MsgBox("Files Deleted", vbOKOnly, "File Deletion")
ElseIf result = MsgBoxResult.No Then
Dim file As String = folderPath.Replace(folderPath, String.Empty)
For Each file In files
Dim fileName As String = Path.GetFileName(file)
lstFiles.Items.Add(fileName)
Next
End If
'Exit Sub
Else
MsgBox("NO Files With" & " f " & "FOUND", vbOKOnly, "File Deletion")
End If
Catch ex As UnauthorizedAccessException
MessageBox.Show("Access to the folder is denied.")
Catch ex As DirectoryNotFoundException
MessageBox.Show("The specified folder does not exist.")
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message)
End Try
End Sub
Private Sub btnDelTemp_Click(sender As Object, e As EventArgs) Handles btnDelTemp.Click
Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Temp\"
Dim result As MsgBoxResult = Nothing
' Get all files in the folder that start with "dd"
Dim files As String() = Directory.GetFiles(folderPath, "dd*")
lstFiles.Items.Clear()
' Check if any files are found
If files.Length = 0 Then
result = MsgBox("No Files to Delete", vbOKOnly, "File Deletion")
Exit Sub
End If
result = MsgBox("Yes to Delete Files" & vbCrLf & "NO to EXIT", vbYesNo, "File Deletion")
If result = MsgBoxResult.Yes Then
For Each file As String In files
lstFiles.Items.Add(file.Replace(folderPath, String.Empty))
' Send file to recycle bin
My.Computer.FileSystem.DeleteFile(file, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
Next
MsgBox("Files Deleted", vbOKOnly, "File Deletion")
ElseIf result = MsgBoxResult.No Then
MsgBox("NO Files DELETED", vbOKOnly, "File Deletion")
Exit Sub
End If
End Sub
Private Sub btnOpenRB_Click(sender As Object, e As EventArgs) Handles btnOpenRB.Click
lstFiles.Items.Clear()
Dim userSid As String = WindowsIdentity.GetCurrent().User.Value
' Construct the path to the Recycle Bin for the current user
Dim recycleBinPath As String = String.Format("C:\$Recycle.Bin\{0}", userSid)
' Start a new process to open Windows Explorer at the Recycle Bin path
Process.Start("explorer.exe", recycleBinPath)
End Sub
End Class
A new auxiliary method, IsListBoxInitialized, is added to check whether the ListBox control has been initialized. Avoid checking whether lstFiles is empty every time when calling other methods.
In the btnVCache_Click and btnVTemp_Click methods, clear the contents of the ListBox and try to open the specified cache folder path using explorer.exe.
In btnDelCache_Click and btnDelTemp_Click methods, asynchronous processing (Async) is introduced to improve the user experience. Use Task.Run to delete files in the background thread.
A new button click event btnOpenRB_Click is added to open the recycle bin. This is achieved by starting explorer.exe and passing in the special path shell:: {645ff040-5081-11b-9F08-00aa002f954e}.
Error handling logic has been added in many places, such as the case that the file does not exist and access is denied, which improves the robustness and user-friendliness of the program.
Public Class Form1
Private Const CacheFolderPath As String = "C:\Users\user\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data"
Private Const TempFolderPath As String = "C:\Users\user\AppData\Local\Temp\"
Public Sub New()
InitializeComponent()
Me.KeyPreview = True
End Sub
Private Function IsListBoxInitialized() As Boolean
If lstFiles Is Nothing Then
MessageBox.Show("ListBox control 'lstFiles' is not initialized.")
Return False
End If
Return True
End Function
Private Sub frmStart_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If Asc(e.KeyChar) = 27 Then
Application.Exit()
End If
End Sub
Private Sub btnVCache_Click(sender As Object, e As EventArgs) Handles btnVCache.Click
If Not IsListBoxInitialized() Then Return
lstFiles.Items.Clear()
If Not Directory.Exists(CacheFolderPath) Then
MessageBox.Show("Cache folder path does not exist.")
Return
End If
Try
Process.Start("explorer.exe", CacheFolderPath)
Catch ex As Exception
MessageBox.Show("Could not open folder: " & ex.Message)
End Try
End Sub
Private Sub btnVTemp_Click(sender As Object, e As EventArgs) Handles btnVTemp.Click
If Not IsListBoxInitialized() Then Return
lstFiles.Items.Clear()
If Not Directory.Exists(TempFolderPath) Then
MessageBox.Show("Temp folder path does not exist.")
Return
End If
Try
Process.Start("explorer.exe", TempFolderPath)
Catch ex As Exception
MessageBox.Show("Could not open folder: " & ex.Message)
End Try
End Sub
Private Async Sub btnDelCache_Click(sender As Object, e As EventArgs) Handles btnDelCache.Click
If Not IsListBoxInitialized() Then Return
Dim files As IEnumerable(Of String) = Directory.EnumerateFiles(CacheFolderPath, "f*")
lstFiles.Items.Clear()
Try
If files.Any() Then
Dim fileCount As Integer = files.Count()
Dim result = MessageBox.Show($"Found {fileCount} files. Proceed with deletion?", "File Deletion", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
For Each fileName In files
Dim fC As String = Path.GetFileName(fileName)
lstFiles.Items.Add(fC)
Await Task.Run(Sub() System.IO.File.Delete(fileName))
Next
MessageBox.Show("Files Deleted", "File Deletion", MessageBoxButtons.OK)
Else
For Each file In files
lstFiles.Items.Add(Path.GetFileName(file))
Next
End If
Else
MessageBox.Show("No files with 'f' found", "File Deletion", MessageBoxButtons.OK)
End If
Catch ex As UnauthorizedAccessException
MessageBox.Show("Access to the folder is denied.")
Catch ex As DirectoryNotFoundException
MessageBox.Show("The specified folder does not exist.")
Catch ex As IOException
MessageBox.Show("An error occurred: " & ex.Message)
End Try
End Sub
Private Async Sub btnDelTemp_Click(sender As Object, e As EventArgs) Handles btnDelTemp.Click
If Not IsListBoxInitialized() Then Return
Dim files As String() = Directory.GetFiles(TempFolderPath, "dd*")
lstFiles.Items.Clear()
If files.Length = 0 Then
MessageBox.Show("No files to delete", "File Deletion", MessageBoxButtons.OK)
Return
End If
Dim result = MessageBox.Show($"Found {files.Length} files. Proceed with deletion?", "File Deletion", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
Try
For Each file In files
lstFiles.Items.Add(Path.GetFileName(file))
Await Task.Run(Sub() System.IO.File.Delete(file))
Next
MessageBox.Show("Files Deleted", "File Deletion", MessageBoxButtons.OK)
Catch ex As UnauthorizedAccessException
MessageBox.Show("Access to the folder is denied.")
Catch ex As DirectoryNotFoundException
MessageBox.Show("The specified folder does not exist.")
Catch ex As IOException
MessageBox.Show("An error occurred: " & ex.Message)
End Try
Else
MessageBox.Show("No files deleted", "File Deletion", MessageBoxButtons.OK)
End If
End Sub
Private Sub btnOpenRB_Click(sender As Object, e As EventArgs) Handles btnOpenRB.Click
If Not IsListBoxInitialized() Then Return
lstFiles.Items.Clear()
Try
Process.Start("explorer.exe", "shell:::{645FF040-5081-101B-9F08-00AA002F954E}")
Catch ex As Exception
MessageBox.Show("Could not open Recycle Bin: " & ex.Message)
End Try
End Sub
End Class