主要目的:对系统回收站的文件进行操作。
首先添加引用,引入shell32.dll。(一般在C:\Windows\System32中)
下面是写的一个函数,直接调用即可(需要配置好listview控件)
private void GetRecycleBin()
{
Shell shell = new Shell();
Folder recycleBin = shell.NameSpace(10);
foreach(FolderItem2 recfile in recycleBin.Items())
{
ListViewItem lvi = new ListViewItem();
//recfile.Name文件名称
lvi.SubItems.Add(recfile.Name);
//recfile.Path文件目录
lvi.SubItems.Add(recfile.Path);
//recfile.Verbs().Item(0).DoIt();//还原文件
//recfile.Verbs().Item(1).DoIt();//剪切
//recfile.Verbs().Item(2).DoIt();//删除
//recfile.Verbs().Item(3).DoIt();//属性
//其他功能自己扩展了。
this.listView1.Items.Add(lvi);
}
}代码来自:https://www.cnblogs.com/backkoms/p/4814968.html
在使用上面的函数时,会出现询问窗口(每个文件都会询问一次,想想就可怕)
通过SHEmptyRecycleBin函数实现可以实现清空回收站的功能。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ClearRecycle
{
public partial class Form1 : Form
{
const int SHERB_NOCONFIRMATION = 0x000001; //不显示确认删除的对话框
const int SHERB_NOPROGRESSUI = 0x000002; //不显示删除过程的进度条
const int SHERB_NOSOUND = 0x000004; //当删除完成时,不播放声音
public Form1()
{
InitializeComponent();
}
[DllImportAttribute("shell32.dll")] //声明API函数
private static extern int SHEmptyRecycleBin(IntPtr handle, string root, int falgs);
//点击"清空回收站"按钮
private void button1_Click(object sender, EventArgs e)
{
SHEmptyRecycleBin(this.Handle, "", SHERB_NOCONFIRMATION + SHERB_NOPROGRESSUI + SHERB_NOSOUND);
}
}
}代码来自:https://blog.csdn.net/Eastmount/article/details/18414935
#转载请注明出处!
快来制作你的简历吧 ,请猛戳这里→点击在线制作
宝塔服务器面板,一键全能部署及管理,送你3188元礼包。请猛戳这里→点我领取



