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