15

I have an application which uses the FOF_ALLOWUNDO with SHFileOperation in order to move files to the recycle bin.

Some removable drives do not have a recycle bin. In this case SHFileOperation deletes the files directly. I want to give a warning to the user that the files are going to be deleted directly.

In order to do this I need to know if the drive has a recycle bin.

ggurbet
  • 82
  • 12
Norbert Willhelm
  • 2,579
  • 1
  • 23
  • 33
  • 1
    You can find your answer here http://stackoverflow.com/questions/1585295/how-can-i-tell-that-a-directory-is-the-recycle-bin-in-c – Senad Meškin Oct 10 '11 at 20:08
  • BoltClock, the correct way would probably to give an option to undo the delete by not actually deleting but moving somewhere else, despite no recycle bin being present. A warning is something no user ever reads, no matter how helpful it may be. The idea is nice and I support it, but it's no different from nearly every other message box a user gets in their face – it will just go unread and the complaints come anyway. – Joey Oct 10 '11 at 20:32
  • @Joey: You're right. Just reminded me of Gmail's prized undo feature. – BoltClock Oct 10 '11 at 20:41

2 Answers2

7

Use FOF_WANTNUKEWARNING.

Send a warning if a file is being permanently destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

I found a function called SHQueryRecycleBin when I looked at the functions exported by shell32.dll.

If the drive specified in pszRootPath has a recycle bin the function returns 0 otherwise it returns -2147467259.

I'm going to use this function via PInvoke.

I used the P/Invoke Interop Assistant to create the PInvoke code.

Here is the code of my function 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;
    }
Norbert Willhelm
  • 2,579
  • 1
  • 23
  • 33