调用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();
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
... {
[FieldOffset(0)] public int cbSize ;
[FieldOffset(4)] public long i64Size ;
[FieldOffset(12)] public long i64NumItems ;
}
// 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 ;
public struct SHQUERYRBINFO
... {
[FieldOffset(0)] public int cbSize ;
[FieldOffset(4)] public long i64Size ;
[FieldOffset(12)] public long i64NumItems ;
}
// 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();
}
/// 查询回收站信息
/// </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();
}