Csharp实现回收站清空

调用shell32.dll 实现回收站清空。

首先用DllImport将函数从shell32.dll中导入。 

        [DllImport( " shell32.dll " )]
        
static   extern   int  SHQueryRecycleBin( string  pszRootPath,  ref  SHQUERYRBINFO pSHQueryRBInfo);
        [DllImport(
" shell32.dll " )]
        
static   extern   int  SHEmptyRecycleBin(IntPtr hWnd,  string  pszRootPath, uint  dwFlags);
        [DllImport(
" shell32.dll " )]
        
static   extern   int  SHUpdateRecycleBinIcon();

SHQueryRecycleBin是回收站信息查询函数;SHEmptyRecycleBin是回收站清空函数;SHUpdateRecycleBinIcon是回收站图标还原函数。然后还得定义SHQUERYRBINFO结构,是用来保存回收站信息的,还有几个常量,是用于SHEmptyRecycleBin函数的最后一个参数的,

 

        [StructLayout(LayoutKind.Explicit, Size = 20 )]
            
public   struct  SHQUERYRBINFO
        
...

        
//      No dialog box confirming the deletion of the objects will be displayed.
         const   int  SHERB_NOCONFIRMATION  =   0x00000001 ;
        
//      No dialog box indicating the progress will be displayed. 
         const   int  SHERB_NOPROGRESSUI  =   0x00000002 ;
        
//      No sound will be played when the operation is complete. 
         const   int  SHERB_NOSOUND  =   0x00000004 ;

然后就可以调用这几个函数进行回收站清空了,

 

         /// <summary>
        
/// 查询回收站信息
        
/// </summary>
        
/// <param name="rootPath">要查询的回收站文件夹路径,当为空时查询回收站</param>
        
/// <param name="sqrbi">存储回收站信息</param>
        
/// <returns>返回零表示操作成功</returns>

         public   static   int  QueryRecycleBin( string  rootPath, ref  SHQUERYRBINFO sqrbi)
        
{
            
return SHQueryRecycleBin(rootPath,ref sqrbi);
        }

        
/// <summary>
        
/// 清空回收站
        
/// </summary>
        
/// <param name="rootPath">要清空的回收站文件夹的路径,当为空时清空回收站</param>
        
/// <returns>返回零表示操作成功</returns>

         public   static   int  EmptyRecycleBin( string  rootPath)
        
{
            
return SHEmptyRecycleBin(IntPtr.Zero, rootPath, 
                SHERB_NOCONFIRMATION 
| SHERB_NOPROGRESSUI | SHERB_NOSOUND);
        }

        
/// <summary>
        
/// 还原回收站图标
        
/// </summary>
        
/// <returns></returns>

         public   static   int  UpdateRecycleBinIcon()
        
{
            
return SHUpdateRecycleBinIcon();
        }

在程序了,首先调用QueryRecycleBin函数查询,如果回收站的字节数或者项目数不为零,就调用EmptyRecycleBin函数进行清空,如果清空的过程中发生错误,调用UpdateRecycleBinIcon函数将回收站的图标还原。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值