Я создаю приложение, в котором мне нужно хранить данные (в формате json) в файле на физическом устройстве.
Проблема, с которой я сталкиваюсь, заключается в том, что, помещая файлы в папку ресурсов, они становятся доступными только для чтения, и это полдела, я могу их прочитать, но тогда где мне разместить файлы, чтобы сделать их доступными для записи?
Когда я перемещаю их в specialFolder.Personal, меня встречают нулевые ссылочные исключения, а когда я перемещаю их в другое место, меня встречают исключения accessDenied.
Любая помощь приветствуется (я все еще довольно новичок в Xamarin, но у меня есть хороший опыт в C #).
Заранее спасибо.
/// <summary>
/// Method to read all data from player data json file.
/// </summary>
/// <returns></returns>
public List<Player> GetAllPlayerData()
{
return ReadFile(playerdata);
}
^^ This method is returning null.
/// <summary>
/// Method to read JSON data from a specified file path.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private List<Player> ReadFile(string path)
{
if (!(File.Exists(path)))
{
File.Create(path).Dispose();
}
List<Player> data = new List<Player>();
string jsondata = File.ReadAllText(path);
data = JsonConvert.DeserializeObject<List<Player>>(jsondata);
return data;
}
^^ This method reads in the data.
private string playerdata = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "playerdata.json");
private string teamdata = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "team.json");
^^ These are the file paths used to read in the data.
/// <summary>
/// Method to write JSON data to a specified file.
/// </summary>
/// <param name="path"></param>
/// <param name="data"></param>
/// <returns></returns>
private void WriteFile(string path, List<Player> data)
{
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
File.WriteAllText(path, json);
}
^^ Here is the method which rights to files.