Shell API - Cannot move my file in the Recycle Bin if deleted from an alias

76 Views Asked by At

I use this code to delete a file from my local system:

bool Delete(const ::CComPtr<IShellItem>& pShellItem, bool permanently)
{
    ::CComPtr<IFileOperation> pFileOperation;

    // initialize the file operation
    HRESULT hr = ::CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pFileOperation));

    if (FAILED(hr))
        return false;

    // don't show any system error message to user, but returns an error code if delete failed. Also show elevation prompt if required
    DWORD fileOpFlags = FOF_SILENT | FOF_NO_UI | FOF_NOERRORUI | FOF_NOCONFIRMATION | FOFX_EARLYFAILURE | FOFX_SHOWELEVATIONPROMPT;

    // do move file on Recycle Bin instead of deleting it permanently?
    if (!permanently)
        if (WOSHelper_MSWindows::GetWindowsVersion() >= WOSHelper_MSWindows::IEVersion::IE_Win8)
            // Windows 8 introduces the FOFX_RECYCLEONDELETE flag and deprecates the FOF_ALLOWUNDO in favor of FOFX_ADDUNDORECORD
            fileOpFlags |= FOFX_ADDUNDORECORD | FOFX_RECYCLEONDELETE;
        else
            // for Windows 7 and Vista, RecycleOnDelete is the default behavior
            fileOpFlags |= FOF_ALLOWUNDO;

    // prepare the delete operation flags
    hr = pFileOperation->SetOperationFlags(fileOpFlags);

    if (FAILED(hr))
        return false;

    // mark item for deletion
    hr = pFileOperation->DeleteItem(pShellItem, nullptr);

    if (FAILED(hr))
        return false;

    // delete the item
    hr = pFileOperation->PerformOperations();

    if (FAILED(hr))
        return false;

    return true;
}

On my local computer I also mounted an alias disk using this command:

subst V: "C:\MyData"

Now, using my above alias, I need to move a file to the Recycle Bin (this is done by setting the permanently parameter to false while I call my function) from the following path:

V:\MyProject\MyFileToDel.txt

Doing that, the file is well deleted from its containing folder, but it is never moved to the Recycle Bin. On the other hand, when I try to delete my file from its original path:

C:\MyData\MyProject\MyFileToDel.txt

The file is well deleted from its containing folder AND moved to the Recycle Bin, as expected.

However this is the exact same file in the exact same location in the both cases, the only difference is that one is deleted from an alias, and the other from its complete path.

What should I change to make the function to work in the both cases?

0

There are 0 best solutions below