如何使用 C++ 读取 Hololens 上的文件

How to read a file on Hololens with C++

我正在尝试读取正在部署到 Hololens 的 test/debug UWP 应用程序中的文件。我可以使用设备门户将文件放在设备上,但无法找到打开文件的正确路径。

我正在使用 MSFT BasicXrApp_uwp 示例作为基础,并包含具有 FindFileInAppFolder 函数的 FileUtility。这一直找不到文件,并出现错误: "The file should be embeded in app folder in debug build.", 让我知道应用程序文件夹是:

C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles4f83f4-6e13-42e4-8253-71dd3040951cVS.Debug_ARM.mikeh\

364f83f4-6e13-42e4-8253-71dd3040951cVS 部分在设备门户中可识别为用户 Folders/LocalAppData 文件夹,但 Debug_ARM.mikeh 部分在门户中不可见。

我正在使用 C++,如果可能的话,我会尝试在静态的非 uwp 库中读取文件(指出这一点,这样我就不会得到使用 UWP 异步内容的建议,如果可能的话)。

那么,如何将我的文件嵌入到应用程序文件夹中,或者如何放置文件以便我可以阅读它?

这是因为FindFileInAppFolder方法returns的文件夹路径是InstalledLocation of the current package, but what you checked in the device portal is LocalFolder/LocalCacheFolder, for more information about what different between them please see: File access permissions.

how do I embed my file in the app folder, or how do I place the file so I can read it?

您可以通过Device Portal 将您的文件放在LocalState 文件夹中,并通过ApplicationData.LocalFolder 属性 获取此文件夹路径,路径应为:C:\Data\Users\DefaultAccount\AppData\Local\Packages4f83f4-6e13-42e4-8253-71dd3040951c\LocalState。对于如何通过 C++ 访问文件,您可以使用 File access sample

我在这里使用的是答案,因为空间比评论多。 我发现一些额外的有用的东西。我将 cppWinRT nuget 包添加到我的应用程序中。 我确实需要使用“异步的东西”,例如:

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage;

StorageFolder storageFolder= KnownFolders::GetFolderForUserAsync(nullptr, KnownFolderId::PicturesLibrary).get();

这让我找到了我上传到图片库的文件。但是在将路径传递到我现有的库后我无法打开它:

const auto sampleFile = storageFolder.GetFileAsync(fileName).get();
std::wstring path = sampleFile.Path();

MyLibraryCall(to_string(path));

MyLibraryCall 会尝试打开一个 ifstream,即使使用 std::ifstream::in 也会失败。 所以我将文件复制到临时目录,在那里我可以打开它并处理它。 这非常 hacky,但它满足了我的需要,即让我加载一个被 3D 零件查看器拒绝的 .obj 文件。

所有文件名的循环是因为storageFolder.GetFileAsync(fileName).get()如果失败会抛出异常,对我来说我无法正确捕获。

StorageFolder tempFolder = Windows::Storage::ApplicationData::Current().TemporaryFolder();
std::wstring path;
auto  files = tempFolder.GetFilesAsync().get();
for (auto file : files) 
{
  if (file.Name() == fileName) {
  path = file.Path();
  break;
  }
}
if (!path.size()) {
  // hasn't been copied into temp
  StorageFile movedFile = sampleFile.CopyAsync(tempFolder).get(); 
  path = movedFile.Path();
}
MyLibraryCall(to_string(path));

无论如何 - 不是最好的,但希望能帮助其他人寻找一种快速而肮脏的方式来处理 hololens/UWP 应用程序上的文件。