【问题标题】:std::filesystem::directory_iterator in C++/CX UWP app can't find directory?C++/CX UWP 应用程序中的 std::filesystem::directory_iterator 找不到目录?
【发布时间】:2018-12-15 09:25:26
【问题描述】:

我正在编写一个跨平台应用程序,所以我想我会用 C++/CX 分别在 XAML 中做 UI。 Cocoa,以及标准 C++ 的核心。但是,我在访问文档时遇到问题。

我提供了一个FolderPicker 并将路径粘贴到directory_iterator 中,但是目录迭代器没有找到任何文件,如果我在路径上调用exists(),它会显示false

我用谷歌搜索了高低,但网络上的所有内容都告诉我,一旦我拥有 StorageFolder,我应该只能访问这些文件,并且没有任何内容涉及标准 C++17 API。

我必须做些什么才能让标准库访问这些文件?

我使用以下方法打开文件选择器:

FolderPicker    ^picker = ref new FolderPicker;
picker->FileTypeFilter->Append( "*" );
IAsyncOperation<StorageFolder ^> ^storageFolderOp = picker->PickSingleFolderAsync();
auto asyncTask = concurrency::create_task(storageFolderOp);
asyncTask.then([this](StorageFolder ^storageFolder)
               {
                   cout << "Picked directory: " << StdStringFromString(storageFolder->Path) << endl;
                   commandsPathField->Text = storageFolder->Path;
               });

采用此字符串(作为std::string)并尝试列出该目录中的文件的代码:

path    commandsFolderPath(inFolderPath);
if (exists(commandsFolderPath))
{
    directory_iterator    directoryIterator(commandsFolderPath);
    for ( ; directoryIterator != directory_iterator(); ++directoryIterator )
    {
        const directory_entry& currFile = *directoryIterator;
        if (currFile.path().filename().string().compare("data") == 0 || currFile.path().filename().string().find(".") == 0)
        {
            continue;
        }
        load_one_command_folder(currFile.path().string());
    }
}
else
{
    cout << "No directory " << commandsFolderPath.string() << endl;
}

还有我的清单:

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp rescap">

  <Identity
    Name="69b58249-31af-4bb3-95f4-fd339268a557"
    Publisher="CN=Uli"
    Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="69b58249-31af-4bb3-95f4-fd339268a557" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

  <Properties>
<DisplayName>VanguardBotGUI</DisplayName>
<PublisherDisplayName>Uli Kusterer</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
  </Dependencies>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="vanguardbot_win.App">
      <uap:VisualElements
        DisplayName="vanguardbot_win"
        Square150x150Logo="Assets\Square150x150Logo.png"
        Square44x44Logo="Assets\Square44x44Logo.png"
        Description="vanguardbot_win"
        BackgroundColor="transparent">
        <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
        <uap:SplashScreen Image="Assets\SplashScreen.png" />
      </uap:VisualElements>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <rescap:Capability Name="appDiagnostics" />
  </Capabilities>
</Package>

完整的代码在https://github.com/uliwitness/vanguardbot,以防你想运行它并单步执行(只需为 UI 设置用户名/密码,失败就在那之前)。相关文件为windows/MainPage.xaml.cppvanguardbot_win::MainPage::FolderPicker_Click)、common/vanguardbot.cppvanguardbot::connect)和vanguardbot/windows/Package.appxmanifest。解决方案是顶层的vanguardbot_win.sln

【问题讨论】:

  • 我已经添加了相关的代码并添加了一个完整项目的链接,我希望这是一个开始吗?该项目现在基本上是标准的 UWP 空窗口应用程序模板,因为在此之前我从未接触过其他代码。
  • 这是在 STL github 上提出的,并确定这不是 STL 错误(网址如下)。所以看起来我们被 UWP api 困住了,目前它的工作非常糟糕。 github.com/microsoft/STL/issues/1210

标签: uwp visual-studio-2017 uwp-xaml c++-cx std-filesystem


【解决方案1】:

不幸的是,从FolderPicker 返回的文件夹是一个“代理”位置,这意味着对它的所有访问都是通过执行适当安全检查的进程外组件进行的。 WinRT 存储 API 知道如何处理这些代理位置,但标准 CRT / STL 函数不知道(此时)。需要更新这些库以在后台调用较新的 Win32 API,以便正确处理代理位置。

目前,您要么必须使用 Windows.Storage API,要么直接使用 Win32 API,例如可以处理代理位置的 FindFirstFileExFromApp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2021-01-17
    • 2017-02-07
    • 1970-01-01
    • 2019-03-10
    • 2019-10-03
    相关资源
    最近更新 更多