C盘回收站还有缓存垃圾文件也要删除,扫描出来,计算有多大
- User
C盘回收站还有缓存垃圾文件也要删除,扫描出来,计算有多大
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
C盘回收站还有缓存垃圾文件也要删除,扫描出来,计算有多大
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
我将修改代码,增加对C盘回收站和缓存垃圾文件的扫描和清理功能。以下是完整的实现代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace DiskCleaner
{
public partial class MainForm : Form
{
// 添加Windows API函数声明用于操作回收站
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleBinFlags dwFlags);
[Flags]
enum RecycleBinFlags : uint
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000002,
SHERB_NOSOUND = 0x00000004
}
private List<FileInfo> emptyFiles = new List<FileInfo>();
private List<FileInfo> junkFiles = new List<FileInfo>();
private List<FileInfo> cacheFiles = new List<FileInfo>();
private long recycleBinSize = 0;
private bool isScanning = false;
// UI控件声明
private TabControl tabControl;
private TabPage scanTab;
private TabPage cleanTab;
private Button scanButton;
private ProgressBar scanProgressBar;
private Label statusLabel;
private ListView emptyFilesListView;
private ListView junkFilesListView;
private ListView cacheFilesListView;
private Label recycleBinLabel;
private Button cleanButton;
private ProgressBar cleanProgressBar;
private Label cleanStatusLabel;
private Panel summaryPanel;
private Label emptyFilesCountLabel;
private Label junkFilesCountLabel;
private Label cacheFilesCountLabel;
private Label recycleBinSizeLabel;
private Label totalSpaceFreedLabel;
private Label cleanResultLabel;
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
// 基本窗口设置
this.Text = "磁盘清理工具";
this.Size = new Size(900, 700);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
// 创建选项卡控件
tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;
// 扫描选项卡
scanTab = new TabPage("扫描");
scanButton = new Button();
scanButton.Text = "扫描磁盘";
scanButton.Size = new Size(150, 40);
scanButton.Location = new Point(20, 20);
scanButton.Click += ScanButton_Click;
scanProgressBar = new ProgressBar();
scanProgressBar.Size = new Size(700, 30);
scanProgressBar.Location = new Point(20, 80);
statusLabel = new Label();
statusLabel.AutoSize = true;
statusLabel.Location = new Point(20, 120);
statusLabel.Text = "准备就绪";
// 空文件列表
Label emptyFilesLabel = new Label();
emptyFilesLabel.AutoSize = true;
emptyFilesLabel.Location = new Point(20, 150);
emptyFilesLabel.Text = "空文件:";
emptyFilesListView = new ListView();
emptyFilesListView.View = View.Details;
emptyFilesListView.FullRowSelect = true;
emptyFilesListView.GridLines = true;
emptyFilesListView.Size = new Size(800, 120);
emptyFilesListView.Location = new Point(20, 170);
emptyFilesListView.Columns.Add("文件路径", 500);
emptyFilesListView.Columns.Add("大小 (字节)", 100);
emptyFilesListView.Columns.Add("修改日期", 150);
// 垃圾文件列表
Label junkFilesLabel = new Label();
junkFilesLabel.AutoSize = true;
junkFilesLabel.Location = new Point(20, 300);
junkFilesLabel.Text = "垃圾文件:";
junkFilesListView = new ListView();
junkFilesListView.View = View.Details;
junkFilesListView.FullRowSelect = true;
junkFilesListView.GridLines = true;
junkFilesListView.Size = new Size(800, 120);
junkFilesListView.Location = new Point(20, 320);
junkFilesListView.Columns.Add("文件路径", 500);
junkFilesListView.Columns.Add("大小 (字节)", 100);
junkFilesListView.Columns.Add("修改日期", 150);
// 缓存文件列表
Label cacheFilesLabel = new Label();
cacheFilesLabel.AutoSize = true;
cacheFilesLabel.Location = new Point(20, 450);
cacheFilesLabel.Text = "缓存文件:";
cacheFilesListView = new ListView();
cacheFilesListView.View = View.Details;
cacheFilesListView.FullRowSelect = true;
cacheFilesListView.GridLines = true;
cacheFilesListView.Size = new Size(800, 120);
cacheFilesListView.Location = new Point(20, 470);
cacheFilesListView.Columns.Add("文件路径", 500);
cacheFilesListView.Columns.Add("大小 (字节)", 100);
cacheFilesListView.Columns.Add("修改日期", 150);
// 回收站信息
recycleBinLabel = new Label();
recycleBinLabel.AutoSize = true;
recycleBinLabel.Location = new Point(20, 600);
recycleBinLabel.Text = "回收站: 0 字节";
scanTab.Controls.Add(scanButton);
scanTab.Controls.Add(scanProgressBar);
scanTab.Controls.Add(statusLabel);
scanTab.Controls.Add(emptyFilesLabel);
scanTab.Controls.Add(emptyFilesListView);
scanTab.Controls.Add(junkFilesLabel);
scanTab.Controls.Add(junkFilesListView);
scanTab.Controls.Add(cacheFilesLabel);
scanTab.Controls.Add(cacheFilesListView);
scanTab.Controls.Add(recycleBinLabel);
// 清理选项卡
cleanTab = new TabPage("清理");
cleanButton = new Button();
cleanButton.Text = "清理文件";
cleanButton.Size = new Size(150, 40);
cleanButton.Location = new Point(20, 20);
cleanButton.Click += CleanButton_Click;
cleanProgressBar = new ProgressBar();
cleanProgressBar.Size = new Size(700, 30);
cleanProgressBar.Location = new Point(20, 80);
cleanStatusLabel = new Label();
cleanStatusLabel.AutoSize = true;
cleanStatusLabel.Location = new Point(20, 120);
cleanStatusLabel.Text = "准备清理";
summaryPanel = new Panel();
summaryPanel.BorderStyle = BorderStyle.FixedSingle;
summaryPanel.Size = new Size(700, 250);
summaryPanel.Location = new Point(20, 160);
Label summaryTitle = new Label();
summaryTitle.AutoSize = true;
summaryTitle.Location = new Point(10, 10);
summaryTitle.Font = new Font(summaryTitle.Font, FontStyle.Bold);
summaryTitle.Text = "清理摘要:";
emptyFilesCountLabel = new Label();
emptyFilesCountLabel.AutoSize = true;
emptyFilesCountLabel.Location = new Point(10, 40);
emptyFilesCountLabel.Text = "空文件: 0";
junkFilesCountLabel = new Label();
junkFilesCountLabel.AutoSize = true;
junkFilesCountLabel.Location = new Point(10, 70);
junkFilesCountLabel.Text = "垃圾文件: 0";
cacheFilesCountLabel = new Label();
cacheFilesCountLabel.AutoSize = true;
cacheFilesCountLabel.Location = new Point(10, 100);
cacheFilesCountLabel.Text = "缓存文件: 0";
recycleBinSizeLabel = new Label();
recycleBinSizeLabel.AutoSize = true;
recycleBinSizeLabel.Location = new Point(10, 130);
recycleBinSizeLabel.Text = "回收站: 0 字节";
totalSpaceFreedLabel = new Label();
totalSpaceFreedLabel.AutoSize = true;
totalSpaceFreedLabel.Location = new Point(10, 160);
totalSpaceFreedLabel.Text = "释放空间: 0 字节";
cleanResultLabel = new Label();
cleanResultLabel.AutoSize = true;
cleanResultLabel.Location = new Point(10, 190);
cleanResultLabel.Text = "";
summaryPanel.Controls.Add(summaryTitle);
summaryPanel.Controls.Add(emptyFilesCountLabel);
summaryPanel.Controls.Add(junkFilesCountLabel);
summaryPanel.Controls.Add(cacheFilesCountLabel);
summaryPanel.Controls.Add(recycleBinSizeLabel);
summaryPanel.Controls.Add(totalSpaceFreedLabel);
summaryPanel.Controls.Add(cleanResultLabel);
Label warningLabel = new Label();
warningLabel.AutoSize = true;
warningLabel.ForeColor = Color.Red;
warningLabel.Location = new Point(20, 430);
warningLabel.Text = "警告: 请先扫描再清理。系统文件不会被删除。";
cleanTab.Controls.Add(cleanButton);
cleanTab.Controls.Add(cleanProgressBar);
cleanTab.Controls.Add(cleanStatusLabel);
cleanTab.Controls
给出最终的完整代码
以下是完整的最终代码,包含C盘回收站和缓存垃圾文件的扫描、计算大小和清理功能:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace DiskCleanerPro
{
public partial class MainForm : Form
{
// Windows API函数声明用于操作回收站
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleBinFlags dwFlags);
[Flags]
enum RecycleBinFlags : uint
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000002,
SHERB_NOSOUND = 0x00000004
}
private List<FileInfo> emptyFiles = new List<FileInfo>();
private List<FileInfo> junkFiles = new List<FileInfo>();
private List<FileInfo> cacheFiles = new List<FileInfo>();
private long recycleBinSize = 0;
private bool isScanning = false;
// UI控件
private TabControl tabControl;
private TabPage scanTab;
private TabPage cleanTab;
private Button scanButton;
private ProgressBar scanProgressBar;
private Label statusLabel;
private ListView emptyFilesListView;
private ListView junkFilesListView;
private ListView cacheFilesListView;
private Label recycleBinLabel;
private Button cleanButton;
private ProgressBar cleanProgressBar;
private Label cleanStatusLabel;
private Panel summaryPanel;
private Label emptyFilesCountLabel;
private Label junkFilesCountLabel;
private Label cacheFilesCountLabel;
private Label recycleBinSizeLabel;
private Label totalSpaceFreedLabel;
private Label cleanResultLabel;
// 系统目录和缓存位置
private static readonly string[] SystemDirectories = {
@"C:\Windows", @"C:\Program Files", @"C:\Program Files (x86)",
@"C:\ProgramData", Environment.GetFolderPath(Environment.SpecialFolder.System)
};
private static readonly string[] JunkExtensions = {
".tmp", ".temp", ".log", ".old", ".bak", ".dmp", ".chk", ".cache"
};
private static readonly string[] CacheLocations = {
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCache"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCookies")
};
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
// 主窗口设置
this.Text = "磁盘清理专业版";
this.Size = new Size(950, 750);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
// 选项卡控件
tabControl = new TabControl { Dock = DockStyle.Fill };
// 扫描选项卡
scanTab = new TabPage("扫描");
// 扫描按钮
scanButton = new Button {
Text = "扫描磁盘",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
scanButton.Click += ScanButton_Click;
// 进度条
scanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 状态标签
statusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备就绪"
};
// 空文件列表
var emptyFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 150),
Text = "空文件:"
};
emptyFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 170)
};
emptyFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 垃圾文件列表
var junkFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 300),
Text = "垃圾文件:"
};
junkFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 320)
};
junkFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 缓存文件列表
var cacheFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 450),
Text = "缓存文件:"
};
cacheFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 470)
};
cacheFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 回收站信息
recycleBinLabel = new Label {
AutoSize = true,
Location = new Point(20, 600),
Text = "回收站: 0 字节"
};
// 添加扫描选项卡控件
scanTab.Controls.AddRange(new Control[] {
scanButton, scanProgressBar, statusLabel,
emptyFilesLabel, emptyFilesListView,
junkFilesLabel, junkFilesListView,
cacheFilesLabel, cacheFilesListView,
recycleBinLabel
});
// 清理选项卡
cleanTab = new TabPage("清理");
// 清理按钮
cleanButton = new Button {
Text = "清理文件",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
cleanButton.Click += CleanButton_Click;
// 清理进度条
cleanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 清理状态标签
cleanStatusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备清理"
};
// 摘要面板
summaryPanel = new Panel {
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(800, 250),
Location = new Point(20, 160)
};
// 摘要内容
var summaryTitle = new Label {
AutoSize = true,
Location = new Point(10, 10),
Font = new Font(DefaultFont, FontStyle.Bold),
Text = "清理摘要:"
};
emptyFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 40),
Text = "空文件: 0"
};
junkFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 70),
Text = "垃圾文件: 0"
};
cacheFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 100),
Text = "缓存文件: 0"
};
recycleBinSizeLabel = new Label {
AutoSize = true,
Location = new Point(10, 130),
Text = "回收站: 0 字节"
};
totalSpaceFreedLabel = new Label {
AutoSize = true,
Location = new Point(10, 160),
Text = "释放空间: 0 字节"
};
cleanResultLabel = new Label {
AutoSize = true,
Location = new Point(10, 190),
Text = ""
};
// 添加摘要内容
summaryPanel.Controls.AddRange(new Control[] {
summaryTitle,
不清理回收站,代码不完整
以下是完整的最终代码,包含C盘回收站扫描和缓存垃圾文件扫描功能,但不执行回收站清理操作:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace DiskCleanerPro
{
public partial class MainForm : Form
{
// 回收站信息结构
[StructLayout(LayoutKind.Sequential)]
public struct SHQUERYRBINFO
{
public int cbSize;
public long i64Size;
public long i64NumItems;
}
// Windows API函数声明用于查询回收站
[DllImport("shell32.dll")]
static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);
private List<FileInfo> emptyFiles = new List<FileInfo>();
private List<FileInfo> junkFiles = new List<FileInfo>();
private List<FileInfo> cacheFiles = new List<FileInfo>();
private long recycleBinSize = 0;
private bool isScanning = false;
// UI控件
private TabControl tabControl;
private TabPage scanTab;
private TabPage cleanTab;
private Button scanButton;
private ProgressBar scanProgressBar;
private Label statusLabel;
private ListView emptyFilesListView;
private ListView junkFilesListView;
private ListView cacheFilesListView;
private Label recycleBinLabel;
private Button cleanButton;
private ProgressBar cleanProgressBar;
private Label cleanStatusLabel;
private Panel summaryPanel;
private Label emptyFilesCountLabel;
private Label junkFilesCountLabel;
private Label cacheFilesCountLabel;
private Label recycleBinSizeLabel;
private Label totalSpaceFreedLabel;
private Label cleanResultLabel;
// 系统目录和缓存位置
private static readonly string[] SystemDirectories = {
@"C:\Windows", @"C:\Program Files", @"C:\Program Files (x86)",
@"C:\ProgramData", Environment.GetFolderPath(Environment.SpecialFolder.System)
};
private static readonly string[] JunkExtensions = {
".tmp", ".temp", ".log", ".old", ".bak", ".dmp", ".chk", ".cache"
};
private static readonly string[] CacheLocations = {
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCache"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCookies"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "Microsoft", "Windows", "WER"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "CrashDumps")
};
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
// 主窗口设置
this.Text = "磁盘清理专业版";
this.Size = new Size(950, 750);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
// 选项卡控件
tabControl = new TabControl { Dock = DockStyle.Fill };
// 扫描选项卡
scanTab = new TabPage("扫描");
// 扫描按钮
scanButton = new Button {
Text = "扫描磁盘",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
scanButton.Click += ScanButton_Click;
// 进度条
scanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 状态标签
statusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备就绪"
};
// 空文件列表
var emptyFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 150),
Text = "空文件:"
};
emptyFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 170)
};
emptyFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 垃圾文件列表
var junkFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 300),
Text = "垃圾文件:"
};
junkFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 320)
};
junkFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 缓存文件列表
var cacheFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 450),
Text = "缓存文件:"
};
cacheFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 470)
};
cacheFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 回收站信息
recycleBinLabel = new Label {
AutoSize = true,
Location = new Point(20, 600),
Text = "回收站: 扫描中..."
};
// 添加扫描选项卡控件
scanTab.Controls.AddRange(new Control[] {
scanButton, scanProgressBar, statusLabel,
emptyFilesLabel, emptyFilesListView,
junkFilesLabel, junkFilesListView,
cacheFilesLabel, cacheFilesListView,
recycleBinLabel
});
// 清理选项卡
cleanTab = new TabPage("清理");
// 清理按钮
cleanButton = new Button {
Text = "清理文件",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
cleanButton.Click += CleanButton_Click;
// 清理进度条
cleanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 清理状态标签
cleanStatusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备清理"
};
// 摘要面板
summaryPanel = new Panel {
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(800, 250),
Location = new Point(20, 160)
};
// 摘要内容
var summaryTitle = new Label {
AutoSize = true,
Location = new Point(10, 10),
Font = new Font(DefaultFont, FontStyle.Bold),
Text = "清理摘要:"
};
emptyFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 40),
Text = "空文件: 0"
};
junkFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 70),
Text = "垃圾文件: 0"
};
cacheFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 100),
Text = "缓存文件: 0"
};
recycleBinSizeLabel = new Label {
AutoSize = true,
Location = new Point(10, 130),
Text = "回收站: 0 字节"
};
totalSpaceFreedLabel = new Label {
AutoSize = true,
Location = new Point(10, 160),
Text = "释放空间: 0 字节"
};
cleanResultLabel = new Label {
AutoSize = true,
Location = new Point(
点击清理后,回收站的东西还在
以下是修复后的完整代码,确保清理操作不会影响回收站内容,同时优化了扫描和清理功能:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DiskCleanerPro
{
public partial class MainForm : Form
{
// 回收站信息结构
[StructLayout(LayoutKind.Sequential)]
public struct SHQUERYRBINFO
{
public int cbSize;
public long i64Size;
public long i64NumItems;
}
[DllImport("shell32.dll")]
static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);
private List<FileInfo> emptyFiles = new List<FileInfo>();
private List<FileInfo> junkFiles = new List<FileInfo>();
private List<FileInfo> cacheFiles = new List<FileInfo>();
private long recycleBinSize = 0;
private bool isScanning = false;
// UI控件
private TabControl tabControl;
private TabPage scanTab;
private TabPage cleanTab;
private Button scanButton;
private ProgressBar scanProgressBar;
private Label statusLabel;
private ListView emptyFilesListView;
private ListView junkFilesListView;
private ListView cacheFilesListView;
private Label recycleBinLabel;
private Button cleanButton;
private ProgressBar cleanProgressBar;
private Label cleanStatusLabel;
private Panel summaryPanel;
private Label emptyFilesCountLabel;
private Label junkFilesCountLabel;
private Label cacheFilesCountLabel;
private Label recycleBinSizeLabel;
private Label totalSpaceFreedLabel;
private Label cleanResultLabel;
// 系统目录和缓存位置
private static readonly string[] SystemDirectories = {
@"C:\Windows", @"C:\Program Files", @"C:\Program Files (x86)",
@"C:\ProgramData", Environment.GetFolderPath(Environment.SpecialFolder.System)
};
private static readonly string[] JunkExtensions = {
".tmp", ".temp", ".log", ".old", ".bak", ".dmp", ".chk", ".cache"
};
private static readonly string[] CacheLocations = {
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCache"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCookies"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "Microsoft", "Windows", "WER"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "CrashDumps")
};
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
// 主窗口设置
this.Text = "磁盘清理专业版";
this.Size = new Size(950, 750);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
// 选项卡控件
tabControl = new TabControl { Dock = DockStyle.Fill };
// 扫描选项卡
scanTab = new TabPage("扫描");
// 扫描按钮
scanButton = new Button {
Text = "扫描磁盘",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
scanButton.Click += ScanButton_Click;
// 进度条
scanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 状态标签
statusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备就绪"
};
// 空文件列表
var emptyFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 150),
Text = "空文件:"
};
emptyFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 170)
};
emptyFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 垃圾文件列表
var junkFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 300),
Text = "垃圾文件:"
};
junkFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 320)
};
junkFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 缓存文件列表
var cacheFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 450),
Text = "缓存文件:"
};
cacheFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 470)
};
cacheFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 回收站信息
recycleBinLabel = new Label {
AutoSize = true,
Location = new Point(20, 600),
Text = "回收站: 扫描中..."
};
// 添加扫描选项卡控件
scanTab.Controls.AddRange(new Control[] {
scanButton, scanProgressBar, statusLabel,
emptyFilesLabel, emptyFilesListView,
junkFilesLabel, junkFilesListView,
cacheFilesLabel, cacheFilesListView,
recycleBinLabel
});
// 清理选项卡
cleanTab = new TabPage("清理");
// 清理按钮
cleanButton = new Button {
Text = "清理文件",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
cleanButton.Click += CleanButton_Click;
// 清理进度条
cleanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 清理状态标签
cleanStatusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备清理"
};
// 摘要面板
summaryPanel = new Panel {
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(800, 250),
Location = new Point(20, 160)
};
// 摘要内容
var summaryTitle = new Label {
AutoSize = true,
Location = new Point(10, 10),
Font = new Font(DefaultFont, FontStyle.Bold),
Text = "清理摘要:"
};
emptyFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 40),
Text = "空文件: 0"
};
junkFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 70),
Text = "垃圾文件: 0"
};
cacheFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 100),
Text = "缓存文件: 0"
};
recycleBinSizeLabel = new Label {
AutoSize = true,
Location = new Point(10, 130),
Text = "回收站: 0 字节 (不会清理)"
};
totalSpaceFreedLabel = new Label {
AutoSize = true,
Location = new Point(10, 160),
Text = "释放空间: 0 字节"
};
cleanResultLabel = new Label {
AutoSize = true,
Location = new Point(10, 190),
Text = ""
};
// 添加
还是不删除回收站
以下是最终完整代码,确保不会清理回收站内容,仅扫描和显示回收站大小:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DiskCleanerPro
{
public partial class MainForm : Form
{
// 回收站信息结构
[StructLayout(LayoutKind.Sequential)]
public struct SHQUERYRBINFO
{
public int cbSize;
public long i64Size;
public long i64NumItems;
}
[DllImport("shell32.dll")]
static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);
private List<FileInfo> emptyFiles = new List<FileInfo>();
private List<FileInfo> junkFiles = new List<FileInfo>();
private List<FileInfo> cacheFiles = new List<FileInfo>();
private long recycleBinSize = 0;
private bool isScanning = false;
// UI控件
private TabControl tabControl;
private TabPage scanTab;
private TabPage cleanTab;
private Button scanButton;
private ProgressBar scanProgressBar;
private Label statusLabel;
private ListView emptyFilesListView;
private ListView junkFilesListView;
private ListView cacheFilesListView;
private Label recycleBinLabel;
private Button cleanButton;
private ProgressBar cleanProgressBar;
private Label cleanStatusLabel;
private Panel summaryPanel;
private Label emptyFilesCountLabel;
private Label junkFilesCountLabel;
private Label cacheFilesCountLabel;
private Label recycleBinSizeLabel;
private Label totalSpaceFreedLabel;
private Label cleanResultLabel;
// 系统目录和缓存位置
private static readonly string[] SystemDirectories = {
@"C:\Windows", @"C:\Program Files", @"C:\Program Files (x86)",
@"C:\ProgramData", Environment.GetFolderPath(Environment.SpecialFolder.System)
};
private static readonly string[] JunkExtensions = {
".tmp", ".temp", ".log", ".old", ".bak", ".dmp", ".chk", ".cache"
};
private static readonly string[] CacheLocations = {
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCache"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCookies"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "Microsoft", "Windows", "WER"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "CrashDumps")
};
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
// 主窗口设置
this.Text = "磁盘清理专业版";
this.Size = new Size(950, 750);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
// 选项卡控件
tabControl = new TabControl { Dock = DockStyle.Fill };
// 扫描选项卡
scanTab = new TabPage("扫描");
// 扫描按钮
scanButton = new Button {
Text = "扫描磁盘",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
scanButton.Click += ScanButton_Click;
// 进度条
scanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 状态标签
statusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备就绪"
};
// 空文件列表
var emptyFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 150),
Text = "空文件:"
};
emptyFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 170)
};
emptyFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 垃圾文件列表
var junkFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 300),
Text = "垃圾文件:"
};
junkFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 320)
};
junkFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 缓存文件列表
var cacheFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 450),
Text = "缓存文件:"
};
cacheFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(850, 120),
Location = new Point(20, 470)
};
cacheFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 550 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 回收站信息
recycleBinLabel = new Label {
AutoSize = true,
Location = new Point(20, 600),
Text = "回收站: 扫描中... (不会清理)"
};
// 添加扫描选项卡控件
scanTab.Controls.AddRange(new Control[] {
scanButton, scanProgressBar, statusLabel,
emptyFilesLabel, emptyFilesListView,
junkFilesLabel, junkFilesListView,
cacheFilesLabel, cacheFilesListView,
recycleBinLabel
});
// 清理选项卡
cleanTab = new TabPage("清理");
// 清理按钮
cleanButton = new Button {
Text = "清理文件",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
cleanButton.Click += CleanButton_Click;
// 清理进度条
cleanProgressBar = new ProgressBar {
Size = new Size(800, 30),
Location = new Point(20, 80)
};
// 清理状态标签
cleanStatusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备清理"
};
// 摘要面板
summaryPanel = new Panel {
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(800, 250),
Location = new Point(20, 160)
};
// 摘要内容
var summaryTitle = new Label {
AutoSize = true,
Location = new Point(10, 10),
Font = new Font(DefaultFont, FontStyle.Bold),
Text = "清理摘要:"
};
emptyFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 40),
Text = "空文件: 0"
};
junkFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 70),
Text = "垃圾文件: 0"
};
cacheFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 100),
Text = "缓存文件: 0"
};
recycleBinSizeLabel = new Label {
AutoSize = true,
Location = new Point(10, 130),
Text = "回收站: 0 字节 (不会清理)"
};
totalSpaceFreedLabel = new Label {
AutoSize = true,
Location = new Point(10, 160),
Text = "释放空间: 0 字节"
};
cleanResultLabel = new Label {
AutoSize = true,
Location = new Point(10, 190),
Text = ""
};
//
我说的意思是,回收站也要清理。全部清空
以下是完整的最终代码,包含回收站清理功能,可以完全清空回收站:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DiskCleanerPro
{
public partial class MainForm : Form
{
// 回收站API声明
[StructLayout(LayoutKind.Sequential)]
public struct SHQUERYRBINFO
{
public int cbSize;
public long i64Size;
public long i64NumItems;
}
[DllImport("shell32.dll")]
static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleBinFlags dwFlags);
[Flags]
enum RecycleBinFlags : uint
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000002,
SHERB_NOSOUND = 0x00000004
}
private List<FileInfo> emptyFiles = new List<FileInfo>();
private List<FileInfo> junkFiles = new List<FileInfo>();
private List<FileInfo> cacheFiles = new List<FileInfo>();
private long recycleBinSize = 0;
private bool isScanning = false;
// UI控件
private TabControl tabControl;
private TabPage scanTab;
private TabPage cleanTab;
private Button scanButton;
private ProgressBar scanProgressBar;
private Label statusLabel;
private ListView emptyFilesListView;
private ListView junkFilesListView;
private ListView cacheFilesListView;
private Label recycleBinLabel;
private Button cleanButton;
private ProgressBar cleanProgressBar;
private Label cleanStatusLabel;
private Panel summaryPanel;
private Label emptyFilesCountLabel;
private Label junkFilesCountLabel;
private Label cacheFilesCountLabel;
private Label recycleBinSizeLabel;
private Label totalSpaceFreedLabel;
private Label cleanResultLabel;
private CheckBox cleanRecycleBinCheckBox;
// 系统目录和缓存位置
private static readonly string[] SystemDirectories = {
@"C:\Windows", @"C:\Program Files", @"C:\Program Files (x86)",
@"C:\ProgramData", Environment.GetFolderPath(Environment.SpecialFolder.System)
};
private static readonly string[] JunkExtensions = {
".tmp", ".temp", ".log", ".old", ".bak", ".dmp", ".chk", ".cache"
};
private static readonly string[] CacheLocations = {
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCache"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "INetCookies"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "Microsoft", "Windows", "WER"),
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "Local", "CrashDumps")
};
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
// 主窗口设置
this.Text = "磁盘清理专业版";
this.Size = new Size(1000, 800);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
// 选项卡控件
tabControl = new TabControl { Dock = DockStyle.Fill };
// 扫描选项卡
scanTab = new TabPage("扫描");
// 扫描按钮
scanButton = new Button {
Text = "扫描磁盘",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
scanButton.Click += ScanButton_Click;
// 进度条
scanProgressBar = new ProgressBar {
Size = new Size(850, 30),
Location = new Point(20, 80)
};
// 状态标签
statusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备就绪"
};
// 空文件列表
var emptyFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 150),
Text = "空文件:"
};
emptyFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(900, 120),
Location = new Point(20, 170)
};
emptyFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 600 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 垃圾文件列表
var junkFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 300),
Text = "垃圾文件:"
};
junkFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(900, 120),
Location = new Point(20, 320)
};
junkFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 600 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 缓存文件列表
var cacheFilesLabel = new Label {
AutoSize = true,
Location = new Point(20, 450),
Text = "缓存文件:"
};
cacheFilesListView = new ListView {
View = View.Details,
FullRowSelect = true,
GridLines = true,
Size = new Size(900, 120),
Location = new Point(20, 470)
};
cacheFilesListView.Columns.AddRange(new[] {
new ColumnHeader { Text = "文件路径", Width = 600 },
new ColumnHeader { Text = "大小", Width = 100 },
new ColumnHeader { Text = "修改日期", Width = 150 }
});
// 回收站信息
recycleBinLabel = new Label {
AutoSize = true,
Location = new Point(20, 600),
Text = "回收站: 扫描中..."
};
// 添加扫描选项卡控件
scanTab.Controls.AddRange(new Control[] {
scanButton, scanProgressBar, statusLabel,
emptyFilesLabel, emptyFilesListView,
junkFilesLabel, junkFilesListView,
cacheFilesLabel, cacheFilesListView,
recycleBinLabel
});
// 清理选项卡
cleanTab = new TabPage("清理");
// 清理按钮
cleanButton = new Button {
Text = "清理文件",
Size = new Size(150, 40),
Location = new Point(20, 20)
};
cleanButton.Click += CleanButton_Click;
// 清理进度条
cleanProgressBar = new ProgressBar {
Size = new Size(850, 30),
Location = new Point(20, 80)
};
// 清理状态标签
cleanStatusLabel = new Label {
AutoSize = true,
Location = new Point(20, 120),
Text = "准备清理"
};
// 清理回收站复选框
cleanRecycleBinCheckBox = new CheckBox {
Text = "清空回收站",
Checked = true,
AutoSize = true,
Location = new Point(20, 160)
};
// 摘要面板
summaryPanel = new Panel {
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(850, 250),
Location = new Point(20, 190)
};
// 摘要内容
var summaryTitle = new Label {
AutoSize = true,
Location = new Point(10, 10),
Font = new Font(DefaultFont, FontStyle.Bold),
Text = "清理摘要:"
};
emptyFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(10, 40),
Text = "空文件: 0"
};
junkFilesCountLabel = new Label {
AutoSize = true,
Location = new Point(