Reputation: 57
I can't get C++ to work with the "ms-appdata:///roaming/" call to retrieve files
I am currently using cpp to write a Chinese Input Method Editor, and it is packaged as a dll.
Therefore, when I make a call to ifstream to read my Settings File, file permissions are limited by whichever application is active, such as for when a Universal Windows Program, which is sandboxed to it's own folders within AppData, and cannot even read other files, much less write to them. My current difficulty is locating files (specifically, the settings file) within that sandbox in the first place.
For example, this line:
WCHAR* FileName2 = L"C:/Users/Dog/AppData/Local/Packages/Facebook.317180B0BB486_8xx8rvfyw5nnt/RoamingState/Settings.txt";
works fine with
std::ifstream settingsFile;
settingsFile.open(FileName2, std::ios::in ); //this reading is successful for hard-coded path
settingsFile.get(myChar);
settingsFile.close();
when facebook messenger is the active program, but this line does not:
WCHAR* FileName2 = L"ms-appdata:///roaming/Settings.txt";
Even though I can't hard code the path for every UserProfile and UWP directory.
Does anyone know what I could be doing wrong? I am using Visual Studio 2015 Community on Windows 10 and have a universal settings file for x86 and x64 EXEs, and I plan to write a service to copy that settings file to the RoamingState folder of each UWP whenever that file changes.
Upvotes: 2
Views: 1347
Reputation: 2907
Use Windows::Storage::ApplicationData::RoamingFolder::Path property to get a full path to the roaming folder:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefolder.path.aspx
"ms-appdata://" thing works only with WinRT file APIs.
Here's how to access that API from standard C++:
#include <cstdint>
#include <string>
#include <windows.storage.h>
#include <wrl.h>
using namespace ABI::Windows::Storage;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
std::wstring GetRoamingFolderPath()
{
ComPtr<IApplicationDataStatics> appDataStatics;
auto hr = RoGetActivationFactory(HStringReference(L"Windows.Storage.ApplicationData").Get(), __uuidof(appDataStatics), &appDataStatics);
if (FAILED(hr)) throw std::runtime_error("Failed to retrieve application data statics");
ComPtr<IApplicationData> appData;
hr = appDataStatics->get_Current(&appData);
if (FAILED(hr)) throw std::runtime_error("Failed to retrieve current application data");
ComPtr<IStorageFolder> roamingFolder;
hr = appData->get_RoamingFolder(&roamingFolder);
if (FAILED(hr)) throw std::runtime_error("Failed to retrieve roaming folder");
ComPtr<IStorageItem> folderItem;
hr = roamingFolder.As(&folderItem);
if (FAILED(hr)) throw std::runtime_error("Failed to cast roaming folder to IStorageItem");
HString roamingPathHString;
hr = folderItem->get_Path(roamingPathHString.GetAddressOf());
if (FAILED(hr)) throw std::runtime_error("Failed to retrieve roaming folder path");
uint32_t pathLength;
auto roamingPathCStr = roamingPathHString.GetRawBuffer(&pathLength);
return std::wstring(roamingPathCStr, pathLength);
}
Upvotes: 4