是否可以使用stdio函数读取Windows应用商店应用中的资源?

时间:2013-11-18 06:03:47

标签: c++ windows-8 windows-store-apps

我正在将用标准C ++编写的现有应用程序移植到Windows 8.我尝试使用WRL来读取资产,但它是大PITA并且不适合我正在使用的多线程模型。 C stdio函数似乎在开发机器上也能正常工作,但在将应用程序发布到Windows应用商店后它们仍能正常工作吗?

下面的代码获取文件系统上资产的完整路径,并使用它来打开文件:

auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/Logo.png");
concurrency::create_task(Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(uri))
.then([](Windows::Storage::StorageFile^ file)
{
    FILE* f;
    auto path = file->Path->Data();
    auto e = _wfopen_s(&f, path, L"rb");
});

1 个答案:

答案 0 :(得分:2)

Windows应用商店应用始终具有对其安装目录的读取权限以及对其本地数据目录的读/写访问权限。即使将应用程序发布到商店,也可以使用stdio函数访问这些位置的文件。

无法使用stdio访问要求清单文件中的条目访问的已知文件夹,例如文档库。

应用的安装和本地数据文件夹是:

Windows::ApplicationModel::Package::Current->InstalledLocation
Windows::Storage::ApplicationData::Current->LocalFolder

获取已安装位置的资产文件的文件系统路径的示例:

std::wstring wpath = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
wpath += L"\\Assets\\Logo.png";

FILE* f = nullptr;
errno_t e = _wfopen_s(&f, wpath.c_str(), L"rb");

使用stdio for Windows Phone应用程序也可以访问这些位置。 Windows应用商店应用也可以使用stdio访问RoamingFolder和TemporaryFolder,这些不适用于Windows Phone应用。更多信息可以在MSDN上找到:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.package.installedlocation.aspx http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.aspx