我有一个应用程序将 FOF_ALLOWUNDO 与 SHFileOperation 一起使用,以便将文件移动到回收站。
某些可移动驱动器没有回收站。在这种情况下 SHFileOperation 直接删除文件。我想向用户发出警告,文件将被直接删除。
为此,我需要知道驱动器是否有回收站。
我有一个应用程序将 FOF_ALLOWUNDO 与 SHFileOperation 一起使用,以便将文件移动到回收站。
某些可移动驱动器没有回收站。在这种情况下 SHFileOperation 直接删除文件。我想向用户发出警告,文件将被直接删除。
为此,我需要知道驱动器是否有回收站。
使用 FOF_WANTNUKEWARNING。
如果文件在删除操作期间被永久破坏而不是回收,则发送警告。此标志部分覆盖 FOF_NOCONFIRMATION。
在查看shell32.dll导出的函数时,我发现了一个名为SHQueryRecycleBin的函数。
如果pszRootPath中指定的驱动器有回收站,则函数返回 0,否则返回 -2147467259。
我将通过 PInvoke 使用此功能。
我使用 P/Invoke Interop Assistant 来创建 PInvoke 代码。
这是我的函数DriveHasRecycleBin的代码:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct SHQUERYRBINFO
{
/// DWORD->unsigned int
public uint cbSize;
/// __int64
public long i64Size;
/// __int64
public long i64NumItems;
}
/// Return Type: HRESULT->LONG->int
///pszRootPath: LPCTSTR->LPCWSTR->WCHAR*
///pSHQueryRBInfo: LPSHQUERYRBINFO->_SHQUERYRBINFO*
[System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint = "SHQueryRecycleBinW")]
private static extern int SHQueryRecycleBinW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)] string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);
public bool DriveHasRecycleBin(string Drive)
{
SHQUERYRBINFO Info = new SHQUERYRBINFO();
Info.cbSize = 20; //sizeof(SHQUERYRBINFO)
return SHQueryRecycleBinW(Drive, ref Info) == 0;
}