个性化阅读
专注于IT技术分析

如何使用C#.NET以编程方式清除回收站

点击下载

本文概述

无论是什么原因, 至少出于学习目的(或者在你正在维护应用程序的设计中), 用于清理回收站的此代码片段确实非常有用且易于实现。循序渐进, 只需几分钟, 你就可以准备清洁回收站。

清洁回收站

首先, 在类顶部包含对useop语句的对InteropServices的引用, 以便稍后在类中使用DllImport方法。

using System.Runtime.InteropServices;
using System.Windows.Forms; // As we are using windows forms, we use this namespace to create a dialog to confirm our action

我们将创建一个枚举变量, 该变量将包含我们的clear函数期望作为参数的代码。

enum RecycleFlags : uint { 
   SHRB_NOCONFIRMATION = 0x00000001, // Don't ask confirmation
   SHRB_NOPROGRESSUI = 0x00000002, // Don't show any windows dialog
   SHRB_NOSOUND = 0x00000004 // Don't make sound, ninja mode enabled :v
}

然后, 我们将创建清除回收站的函数, 我们需要导入Shell32.dll, 因此为了在.dll中使用SHEmptyRecycleBin函数, 我们在System.Runtime.InteropServices命名空间之前添加了该函数, 现在你只需要声明它。

[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);

既然已经声明了SHEmptyRecycleBin方法, 我们就可以使用它最终清理回收站。我们将添加一个触发功能的按钮, 并使用以下代码创建一个确认对话框, 最后清理回收站。

// On click event of the button
private void button1_Click(object sender, EventArgs e)
{
    DialogResult result;
    result = MessageBox.Show("Are you sure you want to delete all the items in recycle bin", "Clear recycle bin", MessageBoxButtons.YesNo);

    // If accepted, continue with the cleaning
    if (result == DialogResult.Yes)
    {
        try
        {
            // Execute the method with the required parameters
            uint IsSuccess = SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHRB_NOCONFIRMATION);
            MessageBox.Show("The recycle bin has been succesfully recycled !", "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {   
            // Handle exceptions
            MessageBox.Show("The recycle bin couldn't be recycled" + ex.Message, "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }
}
回收站

完成课程

这是你班级的样子, 请注意, 这是一个空的winforms项目, 假定你只有一个带有button1标识符的按钮, 并且在单击该按钮时会清理回收站:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace EmptyRecycleBin
{
    public partial class Form1 : Form
    {
        enum RecycleFlags : int
        {
            SHRB_NOCONFIRMATION = 0x00000001, // Don't ask for confirmation
            SHRB_NOPROGRESSUI = 0x00000001, // Don't show progress
            SHRB_NOSOUND = 0x00000004 // Don't make sound when the action is executed
        }

        [DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
        static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Are you sure you want to delete all the items in recycle bin", "Clear recycle bin", MessageBoxButtons.YesNo);

            // If accepted, continue with the cleaning
            if (result == DialogResult.Yes)
            {
                try
                {
                    // Execute the method with the required parameters
                    uint IsSuccess = SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHRB_NOCONFIRMATION);
                    MessageBox.Show("The recycle bin has been succesfully recycled !", "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    // Handle exceptions
                    MessageBox.Show("The recycle bin couldn't be recycled" + ex.Message, "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }
    }
}

玩得开心 !

赞(0)
未经允许不得转载:srcmini » 如何使用C#.NET以编程方式清除回收站

评论 抢沙发

评论前必须登录!