Reputation: 14002
I'm pretty new to file handling in UWP, and it feels really intricate. I don't know how to start. I thought about "learn by doing": I failed miserably.
The goal:
I would like store an object into a Json file, into a well known location. My object is of type "MySettings
" and I would like to save it, for instance, to "ms-appx:///myconfig.json"
.
How can I do it?
Another question: will this file be editable from outside my application easily, like in classic Desktop applications? I thought about editing the settings with a text editor, for changing settings quickly without rebuilding my app.
Upvotes: 1
Views: 3672
Reputation: 15758
ms-appx:///
represents app's install directory which is read-only. So we can't a store Json file in such location. I'd suggest you store Json files in application data locations such as app's local data folder (ms-appdata:///local/
). For more info, please see File access permissions. And following is a simple sample with using Json.NET:
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
// serialize JSON to a string
string json = JsonConvert.SerializeObject(product);
// write string to a file
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myconfig.json");
await FileIO.WriteTextAsync(file, json);
Once the file is stored, you should be able to find it under
%USERPROFILE%\AppData\Local\Packages\{Package family name}\LocalState
folder and you can edit it with a text editor easily. For more info, you may refer to my previous answer here.
Upvotes: 6