我有一个应用程序,使用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;
}