转:C#清除回收站

SHEmptyRecycleBin是一个内核API方法,该方法能够清空回收站中的文件,该方法在C#中需要手动的引入方法所在的类库.该方法在C#中的声明语法如下:

[DllImportAttrbute("shell32.dll")]   //声明API函数
private static extern int SHEmptyRecycleBin(IntPtr handle,string root,int falgs);

参数说明如下:
handle:父窗口句柄.
root:将要清空的回收站的地址,如果为Null值时将清空所有驱动器上的回收站.
falgs:用于清空回收站的功能参数.
注意:因为调用了Windows API,所以要添加对System.Runtime.IneropServices命名空间的引用;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImportAttribute("shell32.dll")]//声明API函数
         private static extern int SHEmptyRecycleBin(IntPtr handle, string root, int falgs);
        const int SHERB_NOCONFIRMATION = 0x000001;  // 整型常量在API中表地删除时没有确认对话框
         const int SHERB_NOPROGRESSUI = 0x000002;    // 在API中表示不显示删除进度条
         const int SHERB_NOSOUND = 0x000004;         // 在API中表示删除完毕时不播放声音

         private void button1_Click(object sender, EventArgs e)
        {
            SHEmptyRecycleBin(this.Handle, "", SHERB_NOCONFIRMATION + SHERB_NOPROGRESSUI + SHERB_NOSOUND);
        }
    }
原文地址:https://www.cnblogs.com/lusunqing/p/3150071.html