Windows 回收站實際儲存於各磁碟根目錄的隱藏資料夾 Recycler (FAT32) 或 Recycled (NTFS, 舊版) / $Recycle.Bin (Vista+)。本文示範使用 System.IO.Directory 與 File 類別列舉並刪除檔案,並說明現代作業系統的路徑差異與權限考量。
using System;
using System.IO;
using System.Windows.Forms;
public class RecycleCleaner
{
public static void Main()
{
string recyclePath = @"C:\Recycled"; // 僅適用舊版 NTFS
if (!Directory.Exists(recyclePath))
{
MessageBox.Show("找不到回收站資料夾");
return;
}
string[] files = Directory.GetFiles(recyclePath);
if (files.Length == 0)
{
MessageBox.Show("回收站已是空的");
return;
}
DialogResult r = MessageBox.Show(
$"確定要永久刪除 {files.Length} 個檔案?",
"清空回收站", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (r == DialogResult.Yes)
{
foreach (string file in files)
{
try
{
File.Delete(file);
}
catch (Exception ex)
{
MessageBox.Show($"刪除失敗:{file}\n{ex.Message}");
}
}
MessageBox.Show("清空完成");
}
}
}
using System;
using System.IO;
using System.Windows.Forms;
public class ModernRecycleCleaner
{
public static void Main()
{
// 取得所有磁碟機的回收站路徑
string[] drives = Environment.GetLogicalDrives();
int totalDeleted = 0;
foreach (string drive in drives)
{
string recyclePath = Path.Combine(drive, "$Recycle.Bin"); // Vista+
if (!Directory.Exists(recyclePath))
recyclePath = Path.Combine(drive, "Recycler"); // 舊版相容
if (!Directory.Exists(recyclePath)) continue;
try
{
// 回收站內部結構為 SID 子資料夾,需遞迴刪除
foreach (string sidDir in Directory.GetDirectories(recyclePath))
{
foreach (string file in Directory.GetFiles(sidDir, "*", SearchOption.AllDirectories))
{
try
{
File.SetAttributes(file, FileAttributes.Normal); // 移除隱藏/系統/唯讀
File.Delete(file);
totalDeleted++;
}
catch { /* 忽略存取中/權限不足 */ }
}
// 嘗試刪除空的 SID 資料夾
try { Directory.Delete(sidDir, true); } catch { }
}
}
catch (UnauthorizedAccessException)
{
MessageBox.Show($"需要管理員權限才能清理 {drive} 回收站");
}
}
MessageBox.Show($"共刪除 {totalDeleted} 個檔案");
}
}
| 技術點 | 說明 |
|---|---|
Directory.GetFiles(path, "*", SearchOption.AllDirectories) | 遞迴取得所有檔案,包含子資料夾 |
File.SetAttributes(file, FileAttributes.Normal) | 回收站檔案常帶 Hidden/System/ReadOnly,刪除前需移除 |
UnauthorizedAccessException | 非管理員無法存取其他使用者 SID 資料夾,需啟動 UAC 提權 |
SHEmptyRecycleBin API | Shell32.dll 官方 API,支援確認對話框、進度 UI、聲音,建議生產環境使用 P/Invoke 呼叫 |
using System;
using System.Runtime.InteropServices;
public static class ShellRecycle
{
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, uint dwFlags);
private const uint SHERB_NOCONFIRMATION = 0x00000001;
private const uint SHERB_NOPROGRESSUI = 0x00000002;
private const uint SHERB_NOSOUND = 0x00000004;
public static void Empty(bool noConfirm = false, bool noProgress = false, bool noSound = false)
{
uint flags = 0;
if (noConfirm) flags |= SHERB_NOCONFIRMATION;
if (noProgress) flags |= SHERB_NOPROGRESSUI;
if (noSound) flags |= SHERB_NOSOUND;
int result = SHEmptyRecycleBin(IntPtr.Zero, null, flags);
if (result != 0)
throw new System.ComponentModel.Win32Exception(result);
}
}
// 使用
ShellRecycle.Empty(noConfirm: true);
<requestedExecutionLevel level="requireAdministrator" /> 要求管理員權限。