C#实用教程-操作回收站

主要目的:对系统回收站的文件进行操作。

首先添加引用,引入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

QQ图片20200826001511.png

在使用上面的函数时,会出现询问窗口(每个文件都会询问一次,想想就可怕)

QQ图片20200826165621.png

通过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元礼包。请猛戳这里→点我领取

本文标题:《C#实用教程-操作回收站》作者:xuanzhe
原文链接:https://blog.xuanzhe.club/?id=12
特别注明外均为原创,转载请注明。

分享到微信

扫描二维码

可在微信查看或分享至朋友圈。

相关文章

发表评论:
验证码

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

你好,朋友

真是美好的一天!

访客信息

  • IP: 18.191.176.66
  • 地点: United StatesOhioDublin

标签列表

站点信息

  • 文章总数:69
  • 页面总数:2
  • 分类总数:19
  • 标签总数:34
  • 评论总数:7
  • 浏览总数:147010
您好,欢迎到访网站!
忘记密码

网站分类

文章归档

歌曲 - 歌手
0:00