public int Add(User user) { if (File.Exists(_settingsLogic.GetPath())) { using (StreamReader sr = new StreamReader(_settingsLogic.GetPath(), true)) { cache = JsonConvert.DeserializeObject <List <User> >(sr.ReadToEnd()); if (cache == null) { cache = new List <User>(); } } } else { cache = new List <User>(); } cache.Add(user); using (StreamWriter sw = new StreamWriter(_settingsLogic.GetPath(), false)) { sw.Write(JsonConvert.SerializeObject(cache)); } return(0); }
public int Add(User user) { if (File.Exists(_settingsLogic.GetPath())) { using (StreamReader sr = new StreamReader(_settingsLogic.GetPath(), true)) { items = JsonConvert.DeserializeObject <List <ItemInFile> >(sr.ReadToEnd()); if (cache == null) { cache = new List <User>(); var users = items.Select(item => item.user); foreach (var item in users) { cache.Add(item); } } } } else { cache = new List <User>(); } cache.Add(user); items.Add(new ItemInFile() { user = user, award = null }); int count = 0; foreach (var item in items) { if (item.user != null) { item.user.Id = count; count++; } } using (StreamWriter sw = new StreamWriter(_settingsLogic.GetPath(), false)) { sw.Write(JsonConvert.SerializeObject(items)); } return(0); }