Reputation: 7
I am trying to save a JSON file in the app folder, i am able to read the data from this file, but i am not able to write data in this file.
data.json is marked as "Content" in the file properties.
class Json
{
private string data;
private const string value1key = "value1";
private const string value2key = "value2";
private const string value3key = "value3";
StorageFile file;
StorageFolder folder;
private void File()
{
folder = Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Data").AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
//file = StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///data.json")).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
file = folder.GetFileAsync("data.json").AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
}
public Json()
{
File();
}
public string Read()
{
return (FileIO.ReadTextAsync(file).AsTask().ConfigureAwait(false).GetAwaiter().GetResult());
}
public async void Save()
{
JsonObject jsonObject = new JsonObject();
jsonObject["value1"] = JsonValue.CreateNumberValue(Data._value1);
jsonObject["value2"] = JsonValue.CreateNumberValue(Data._value2);
jsonObject["value3"] = JsonValue.CreateNumberValue(Data._value3);
string newData = jsonObject.Stringify();
await FileIO.WriteTextAsync(file, newData);
//FileIO.WriteTextAsync(file, newData).AsTask().ConfigureAwait(true).GetAwaiter();
}
}
I got this error: System.UnauthorizedAccessException: 'Denied access. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
If the problem has no solution, is there another way to save application data?
Upvotes: 0
Views: 573
Reputation: 5877
All files in App folder are read-only. So try setting the FileAttributes
as Normal
before writing data in that file. You can do it using below code
File.SetAttributes(file, FileAttributes.Normal);
Upvotes: 0