UWP - Why i get ACCESS DENIED error when writing to "ms-appx:///DataModel/SampleData.json"?


(isoftech) #1

Hi, while i’ve been working with the sample Windows App Store Example project offered by Microsoft (MSDN) which reads data from this file "ms-appx:///DataModel/SampleData.json", i tried to instead write data to that file using the code below:

public static string _data_file_path = "ms-appx:///DataModel/SampleData.json";
Uri dataUri = new Uri(_data_file_path);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
var x = jsonObject.ToString();
// It crushes in this line with ACCESS DENIED ERROR.
// the x variable is a simple json string.
await FileIO.WriteTextAsync(file, x);

thanks in advance for your assistance.

(Yassine) #2

You cannot write into that file, i think it’s in a ReadOnly directory.

you should know that WinRT (UWP) Apps, are limited in accessing your computer’s files, are allowed to access only files at "Application Data", "Application Temporary" and "Application Roaming". so i would suggest that you first copy that file to one of those directories before attempting to write into it.