0

It doesn't crash immediately, the for loop lists a few file paths then I get this exception:

Unhandled exception at 0x76F05608 in testpath.exe: Microsoft C++ exception: std::filesystem::filesystem_error at memory location 0x0044F8A8.

But it doesn't crash if its CSIDL_DESKTOP instead of CSIDL_PROGRAM_FILESX86.

#include <iostream>
#include <filesystem>
#include <shlobj.h>

#pragma comment(lib, "shell32.lib")

namespace fs = std::filesystem;

int main()
{
    CHAR programx86[MAX_PATH];
    HRESULT result = SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL, SHGFP_TYPE_CURRENT, programx86);

    for (auto& p : fs::recursive_directory_iterator(programx86))
    {
        std::cout << p.path().u8string() << std::endl;
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Neuronic
  • 1
  • 2
  • Did you check if `SHGetFolderPath()` returned anything valid? – πάντα ῥεῖ Oct 23 '20 at 15:01
  • Put a `try..catch` around the loop and see what the `filesystem_error` exception's `code`, `what`, `path1` and `path2` are actually telling you. Since you are iterating through ProgramFiles, you likely encountered a file/folder that you don't have permission to access. Also FYI, `CSIDL_DESKTOP` does not represent a filesystem path, you need `CSIDL_DESKTOPDIRECTORY` for that. – Remy Lebeau Oct 23 '20 at 16:38
  • @RemyLebeau yes!. i found out that was my exact problem. but i have no idea how to ignore files/folders that the program has no premission to. the folder that caused me problems "C:\Program Files (x86)\Google\CrashReports" when i go there with explorer it asks for premission to continue for some reason. thx for tip. – Neuronic Oct 24 '20 at 10:21
  • @Neuronic “*I have no idea how to ignore files/folders that the program has no permissions to* - you would have to break up the loop so you can put a `try..catch` around the individual iterator dereferences so you can catch and discard errors and increment the iterator to the next item. – Remy Lebeau Oct 24 '20 at 17:14
  • @RemyLebeau Thx for reply, turns out i can just use `fs::directory_options::skip_permission_denied` as an option. so it would look like `(auto p : fs::recursive_directory_iterator(m_list[2].macro, fs::directory_options::skip_permission_denied)) ` if anyone in future looking for an answer. – Neuronic Oct 24 '20 at 19:25
  • @Neuronic even better, thanks. Good to know. – Remy Lebeau Oct 24 '20 at 19:26

0 Answers0