You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效处理Chrome缓存文件与Visual Studio日志文件?

问题描述

我不确定当前处理这些无用文件的方法是否最优。我编写了一个小型VB.Net程序,用于删除格式为“f_00006a”的Chrome缓存数据文件,以及每次开机和电脑闲置10分钟以上时,Visual Studio(已设置为不请求更新)在Temp文件夹生成的“dd_updateconfiuration_202.log”等一系列dd开头日志文件,这一情况十分烦人。该程序可正常运行,但我觉得为此编写程序有些小题大做,现附上代码,恳请各位提供优化建议。

现有VB.Net代码
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
优化建议

一、替代方案:用任务计划+脚本,无需编写程序

完全不用写VB.Net程序,用Windows自带工具就能解决:

  1. 写两个批处理脚本(或PowerShell脚本)
    • Chrome缓存清理脚本(CleanChromeCache.bat):
      @echo off
      :: 删除Chrome Cache_Data下以f开头的文件,/Q静默删除,/F强制删除
      del /F /Q "C:\Users\Dwight\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data\f*"
      
    • Temp文件夹dd文件清理脚本(CleanVSTempLogs.bat):
      @echo off
      del /F /Q "C:\Users\Dwight\AppData\Local\Temp\dd*"
      
    如果想把文件删到回收站,可以用PowerShell脚本替代:
    # 清理Chrome缓存
    Remove-Item "C:\Users\Dwight\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data\f*" -RecycleBin -ErrorAction SilentlyContinue
    # 清理VS日志
    Remove-Item "C:\Users\Dwight\AppData\Local\Temp\dd*" -RecycleBin -ErrorAction SilentlyContinue
    
  2. 打开「任务计划程序」,创建两个任务:
    • 任务1:开机时触发,执行上述清理脚本
    • 任务2:电脑闲置10分钟时触发,执行清理脚本

二、现有VB.Net代码优化(如果坚持用程序)

  1. 动态获取路径:替换硬编码的用户路径,适配不同用户
    比如用Environment.GetFolderPath获取系统路径:
    Dim localAppData As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
    Dim chromeCachePath As String = Path.Combine(localAppData, "Google\Chrome\User Data\Default\Cache\Cache_Data\")
    Dim tempPath As String = Path.Combine(localAppData, "Temp\")
    
  2. 优化缓存删除逻辑:已经用Directory.EnumerateFiles(folderPath, "f*")获取目标文件,无需再遍历所有文件过滤,直接循环files集合:
    If result = MsgBoxResult.Yes Then
        For Each fileName As String In files
            Dim fileNameOnly As String = Path.GetFileName(fileName)
            lstFiles.Items.Add(fileNameOnly)
            My.Computer.FileSystem.DeleteFile(fileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
        Next
        MsgBox("Files Deleted", vbOKOnly, "File Deletion")
    End If
    
  3. 代码复用:把文件删除、路径拼接、列表更新的逻辑抽成单独方法,避免重复代码
  4. 简化弹窗:去掉多余的“Files Found”弹窗,直接在确认弹窗里显示发现的文件数量,比如MsgBox($"发现{files.Count()}个待删除文件,是否删除?", vbYesNo, "文件删除")
  5. 增强错误处理:增加对文件被占用的捕获(IOException),提示用户文件正在使用,无法删除

三、其他工具替代

用CCleaner这类专业清理工具,自定义清理规则:

  • 添加Chrome Cache_Data目录下的f*文件规则
  • 添加Temp目录下的dd*文件规则
  • 设置自动清理计划,定时或触发条件(开机、闲置)执行清理

内容的提问来源于stack exchange,提问作者Vector

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:41:01