Get RecycleBin path

Flaviu_ 931 Reputation points
2024-05-21T14:36:52.1966667+00:00

I am trying to get the RecycleBin path with the following code:

std::wstring GetRecycleBinPath()
{
	std::wstring path{};
	SHGetSpecialFolderPathW(NULL, path.data(), CSIDL_BITBUCKET, FALSE);
	std::clog << ::GetLastError() << std::endl;
	return path;
}

and

int main()
{
	const auto recycleBinPath = GetRecycleBinPath();
	if (!recycleBinPath.empty())
	{
		std::wcout << L"Recycle Bin Path: " << recycleBinPath << std::endl;
	}
	else
	{
		std::wcerr << L"Failed to get Recycle Bin Path" << std::endl;
	}
	return 0;
}

But I always got: Failed to get Recycle Bin Path. Does it matter if I am not the administrator of the machine?

If not, how can I accomplish this? How to find what is the issue? -> If I queried SHGetSpecialFolderPathW with GetLastError() the outcome was 0 (ERROR_SUCCESS).

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,591 questions
{count} votes

Accepted answer
  1. Michael Taylor 49,781 Reputation points
    2024-05-21T15:10:29.3433333+00:00

    Does your code even compile? data() returns a const string. That isn't compatible with the LPWSTR that the function requires. When I try your test code the compiler fails the call as I would expect.

    std::wstring GetRecycleBinPath()
    {
        std::wstring path;
    
        path.resize(MAX_PATH);
        SHGetSpecialFolderPathW(NULL, &path[0], CSIDL_BITBUCKET, FALSE);
        //SHGetSpecialFolderPathW(NULL, path.data(), CSIDL_BITBUCKET, FALSE);
        std::clog << ::GetLastError() << std::endl;
        return path;
    }
    

    As for the underlying issue, you cannot get the recycling bin using this function. It isn't a real folder and therefore there is no path to return. This applies to any virtual folder actually.

    Since the folder is virtual there is really nothing you can do with the path itself. Can you clarify what you're planning to do with the folder as there is likely an API to handle that (e.g. restore or clear the bin).

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful