0
votes

I am not sure if this is intended but creating a 'Blank App (c++/WinRT)' in VS 2019 and trying something as simple as the below will always gives access denied. Is it that only app dir and local are supported for std:: ? Or am I missing something?

std::filesystem::path file = LR"(C:\Users\name\Pictures\1.png)";
    auto t = std::filesystem::is_regular_file(file);

I have tried broadFileSystemAcess with no luck and various capabilities. I think ultimately the answer is this is something that cannot be done without using RT file access APIs instead of c++ std::

1

1 Answers

1
votes

UWP applications don't have direct access to any of the file system except:

  • Read-only non-exclusive access to the package install folder
  • Read/write access to an application data folder (roaming or local)
  • Read/write to a private temp folder.

All other access is mediated through Windows Runtime 'brokers'.

One solution is to use Windows Runtime APIs to copy the entire file to a temporary folder and then use std::filesystem on the temporary copy.

Here's an example using the pictures folder:

#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Storage.Pickers.h"

using namespace winrt::Windows::Storage;
using namespace winrt::Windows::Storage::Pickers;

FileOpenPicker openPicker;
openPicker.ViewMode(PickerViewMode::Thumbnail);
openPicker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);
openPicker.FileTypeFilter().Append(L".jpg");
openPicker.FileTypeFilter().Append(L".png");

auto file = co_await openPicker.PickSingleFileAsync();
if (file)
{
    auto tempFolder = ApplicationData::Current().TemporaryFolder();
    auto tempFile = co_await file.CopyAsync(tempFolder, file.Name(), NameCollisionOption::GenerateUniqueName);
    if (tempFile)
    {
        std::filesystem::path file = LR"(tempFile.Path().c_str())";
...
        DeleteFile(tempFile.Path().c_str());
    }
}

See Microsoft Docs