本文概述
- 清洁回收站
- 完成课程
清洁回收站 首先, 在类顶部包含对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 confirmationSHRB_NOPROGRESSUI = 0x00000002, // Don't show any windows dialogSHRB_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 buttonprivate 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 cleaningif (result == DialogResult.Yes){try{// Execute the method with the required parametersuint 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 exceptionsMessageBox.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 confirmationSHRB_NOPROGRESSUI = 0x00000001, // Don't show progressSHRB_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 cleaningif (result == DialogResult.Yes){try{// Execute the method with the required parametersuint 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 exceptionsMessageBox.Show("The recycle bin couldn't be recycled" + ex.Message, "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}}}}}
【如何使用C#.NET以编程方式清除回收站】玩得开心 !
推荐阅读
- 如何使用C#从Windows播放,暂停音乐或转到下一首和上一首曲目(适用于所有Windows音乐播放器)
- 如何下载github存储库的单个文件夹
- XML文档中的![CDATA []]节点有什么作用
- 如何在Windows中使用命令提示符执行symfony 3命令
- 如何解决Windows的git(Powershell和GitHub应用程序)中的”文件名过长”错误
- 如何使用FOSOAuthServerBundle从数据库中清除所有过期的令牌
- 如何使用Doctrine和Symfony 3实现全文搜索(MySql)
- 如何在Symfony 2.8中使用FOSUserBundle实现用户系统
- 如何将Chrome Inspect Tools主题更改为Dark