我正在编写一个跨平台应用程序,因此以为我会在XAML中使用C ++ / CX resp进行UI。可可和标准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.cpp
(vanguardbot_win::MainPage::FolderPicker_Click
),common/vanguardbot.cpp
(vanguardbot::connect
)和vanguardbot/windows/Package.appxmanifest
。解决方案是位于顶层的vanguardbot_win.sln
。
答案 0 :(得分:1)
不幸的是,从FolderPicker
返回的文件夹是“代理”位置,这意味着对它的所有访问都通过执行适当安全检查的进程外组件进行。 WinRT Storage API知道如何处理这些代理位置,但是标准CRT / STL功能尚不支持(此时)。为了正确处理代理位置,需要更新库以调用较新的Win32 API。
现在,您将不得不使用Windows.Storage
API或直接使用可以处理代理位置的Win32 API,例如FindFirstFileExFromApp
。